溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java怎么使用stream判斷兩個list元素的屬性并輸出

發(fā)布時間:2022-06-08 09:17:36 來源:億速云 閱讀:629 作者:iii 欄目:開發(fā)技術

今天小編給大家分享一下java怎么使用stream判斷兩個list元素的屬性并輸出的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

使用stream判斷兩個list元素的屬性并輸出

/**
* 使用stream判斷兩個list中元素不同的item
*/
@Test
public void test1(){
List<Param> stringList1 = new LinkedList<Param>(){{
    add(new Param(1,"1111"));
    add(new Param(2, "2222"));
    add(new Param(3, "3333"));
}};
List<Param> stringList2 = new LinkedList<Param>(){{
    add(new Param(1,"1111"));
    add(new Param(2, "4444"));
    add(new Param(3, "5555"));
}};
// 判斷key相同,value相同的元素
Map<Integer, String> tmpList2 = stringList2.stream().collect(Collectors.toMap(Param::getId, Param::getName));
var tmplist = stringList1.stream().filter(item -> (tmpList2.get(item.getId()) != null && tmpList2.get(item.getId()).equals(item.getName()))).collect(Collectors.toList());
System.out.println(tmplist);
}
@Setter
@Getter
@ToString
@AllArgsConstructor
public static class Param{
private Integer id;
private String name;
}
/**
 * 使用stream判斷兩個list中元素不同的item
 */
@Test
public void test1(){
    List<Param> stringList1 = new LinkedList<Param>(){{
        add(new Param(1,"1111", "b"));
        add(new Param(2, "2222", "c"));
        add(new Param(3, "3333", "a"));
    }};
    List<Param> stringList2 = new LinkedList<Param>(){{
        add(new Param(1,"1111", "c"));
        add(new Param(2, "4444", "b"));
        add(new Param(3, "5555", "a"));
    }};
   // 判斷key相同,value相同的元素
   Map<Integer, String> tmpList2 = stringList2.stream().collect(Collectors.toMap(Param::getId, Param::getName));
   var tmplist = stringList1.stream().filter(item -> (tmpList2.get(item.getId()) != null && tmpList2.get(item.getId()).equals(item.getName()))).collect(Collectors.toList());
   System.out.println(tmplist);
   // 如果需要判斷多個值,直接將對象加入進去
   Map<Integer, Param> tmpList3 = stringList2.stream().collect(Collectors.toMap(Param::getId, Function.identity()));
   var tmplist2 = stringList1.stream().filter(item -> (tmpList3.get(item.getId()) != null && tmpList3.get(item.getId()).getType().equals(item.getType()))).collect(Collectors.toList());
   System.out.println(tmplist2);
}
@Setter
@Getter
@ToString
@AllArgsConstructor
@EqualsAndHashCode
public static class Param{
    private Integer id;
    private String name;
    private String type;
}

stream判斷列表是否包含某幾個元素/重復元素

(需求經過修改過)判斷一個profile是否包含PROFILE-IN-A和PROFILE-IN-B且都是Enable=1打勾的.

既然已經JDK8了,那就用lambda吧,如果是foreach可能比較難處理,用stream的filter則可以這樣做.

核心代碼可以這么寫

int intCheck = profileServiceDtoList.stream().filter(e ->
            "1".equals(e.getEnable())
            &&(("PROFILE-IN-MOSHOW".equals(e.getServiceIdentifier()))||("PROFILE-IN-ADC".equals(e.getServiceIdentifier())))  
    ).collect(Collectors.toList()).size();

代碼SHOW

  1. 新建三個不同類型的profile,其中兩個是要判斷的,一個是干擾的.

  2. 通過steam進行filter,找出是否包含這兩個元素(相當于把要判斷的元素過濾進去)

  3. 判斷l(xiāng)ist的size大小(intCheck>1找到兩個則代表同時出現(xiàn))

public static void main(String[] args) {
    List<ProfileServiceDto> profileServiceDtoList= new ArrayList<>(3);
    
    ProfileServiceDto profileService1 = new ProfileServiceDto();
    profileService1.setServiceId(1001L);
    profileService1.setServiceIdentifier("PROFILE-IN-MOSHOW");
    profileService1.setEnable("1");
    profileServiceDtoList.add(profileService1);
    ProfileServiceDto profileService2 = new ProfileServiceDto();
    profileService2.setServiceId(1002L);
    profileService2.setServiceIdentifier("PROFILE-IN-ADC");
    profileService2.setEnable("1");
    profileServiceDtoList.add(profileService2);
    ProfileServiceDto profileService3 = new ProfileServiceDto();
    profileService3.setServiceId(1003L);
    profileService3.setServiceIdentifier("PROFILE-XXX-ABC");
    profileService3.setEnable("1");
    profileServiceDtoList.add(profileService3);
    int intCheck = profileServiceDtoList.stream().filter(e ->
            "1".equals(e.getEnable())&&(("PROFILE-IN-MOSHOW".equals(e.getServiceIdentifier()))||("PROFILE-IN-ADC".equals(e.getServiceIdentifier())))
    ).collect(Collectors.toList()).size();
    System.out.println("intCheck->"+intCheck);
    
    if(intCheck>1){
        System.error.println("In one profile, cannot contain two more PROFILE-IN profile.");
    }
}

Java stream判斷列表是否包含重復元素

思路是通過一個distinct的list,然后跟原先的list來判斷大小,如果不一致(原先list.size>distinctList.size)則表示有重復元素

        //profileServiceDtoList路上,不累贅
        //多了一個profileService1.setGroupId("A");profileService1.setGroupId("B");profileService3.setGroupId("A");
        List<String> groupList = new ArrayList<>(4);
        profileServiceDtoList.stream().forEach(e -> {
            if ("Y".equals(e.getEnable()) && StringUtils.isNotEmpty(e.getGroupId())) {
                groupList.add(e.getGroupId());
            }
        });
        int distinctGroupSize = groupList.stream().distinct().collect(Collectors.toList()).size();
        if (groupList.size() > distinctGroupSize) {
            throw new ValidationException("100001","In one profile, the services with the same groupId cannot co-exist.");
        }

以上就是“java怎么使用stream判斷兩個list元素的屬性并輸出”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI