欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java使用stream判斷兩個(gè)list元素的屬性并輸出方式

 更新時(shí)間:2022年06月07日 16:48:08   作者:enjoy囂士  
這篇文章主要介紹了java使用stream判斷兩個(gè)list元素的屬性并輸出方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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

/**
* 使用stream判斷兩個(gè)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判斷兩個(gè)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);
   // 如果需要判斷多個(gè)值,直接將對(duì)象加入進(jìn)去
   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判斷列表是否包含某幾個(gè)元素/重復(fù)元素

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

既然已經(jīng)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. 新建三個(gè)不同類型的profile,其中兩個(gè)是要判斷的,一個(gè)是干擾的.
  2. 通過steam進(jìn)行filter,找出是否包含這兩個(gè)元素(相當(dāng)于把要判斷的元素過濾進(jìn)去)
  3. 判斷l(xiāng)ist的size大小(intCheck>1找到兩個(gè)則代表同時(shí)出現(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判斷列表是否包含重復(fù)元素

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

?? ??? ?//profileServiceDtoList路上,不累贅
?? ??? ?//多了一個(gè)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.");
? ? ? ? }

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。 

相關(guān)文章

  • 帶大家認(rèn)識(shí)Java語法之泛型與通配符

    帶大家認(rèn)識(shí)Java語法之泛型與通配符

    使用泛型的目的是利用Java編譯機(jī)制,在編譯過程中幫我們檢測(cè)代碼中不規(guī)范的有可能導(dǎo)致程序錯(cuò)誤的代碼,下面這篇文章主要給大家介紹了關(guān)于Java泛型與通配符的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • SpringBoot實(shí)現(xiàn)redis延遲隊(duì)列的示例代碼

    SpringBoot實(shí)現(xiàn)redis延遲隊(duì)列的示例代碼

    延時(shí)隊(duì)列場(chǎng)景在我們?nèi)粘I(yè)務(wù)開發(fā)中經(jīng)常遇到,它是一種特殊類型的消息隊(duì)列,本文就來介紹一下SpringBoot實(shí)現(xiàn)redis延遲隊(duì)列的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • 利用Java實(shí)體bean對(duì)象批量數(shù)據(jù)傳輸處理方案小結(jié)

    利用Java實(shí)體bean對(duì)象批量數(shù)據(jù)傳輸處理方案小結(jié)

    javabean是對(duì)面向?qū)ο笏枷氲囊环N具體實(shí)施的表現(xiàn),本文重點(diǎn)給大家介紹利用Java實(shí)體bean對(duì)象批量數(shù)據(jù)傳輸處理方案小結(jié),本文通過兩種方案給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2021-05-05
  • Spring表達(dá)式語言SpEL用法詳解

    Spring表達(dá)式語言SpEL用法詳解

    這篇文章主要介紹了spring表達(dá)式語言SpEL用法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • 用SpringBoot+Vue+uniapp小程序?qū)崿F(xiàn)在線房屋裝修管理系統(tǒng)

    用SpringBoot+Vue+uniapp小程序?qū)崿F(xiàn)在線房屋裝修管理系統(tǒng)

    這篇文章主要介紹了用SpringBoot+Vue+uniapp實(shí)現(xiàn)在線房屋裝修管理系統(tǒng),針對(duì)裝修樣板信息管理混亂,出錯(cuò)率高,信息安全性差,勞動(dòng)強(qiáng)度大,費(fèi)時(shí)費(fèi)力等問題開發(fā)了這套系統(tǒng),需要的朋友可以參考下
    2023-03-03
  • java圖形界面之加法計(jì)算器

    java圖形界面之加法計(jì)算器

    這篇文章主要為大家詳細(xì)介紹了java圖形界面之加法計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Java隊(duì)列同步器之CountDownLatch實(shí)現(xiàn)詳解

    Java隊(duì)列同步器之CountDownLatch實(shí)現(xiàn)詳解

    這篇文章主要介紹了Java隊(duì)列同步器之CountDownLatch實(shí)現(xiàn)詳解,CountDownLatch是一個(gè)同步工具類,它允許一個(gè)或多個(gè)線程一直等待,直到其他線程執(zhí)行完后再執(zhí)行,例如,應(yīng)用程序的主線程希望在負(fù)責(zé)啟動(dòng)框架服務(wù)的線程已經(jīng)啟動(dòng)所有框架服務(wù)之后執(zhí)行,需要的朋友可以參考下
    2023-12-12
  • 通過java反射機(jī)制動(dòng)態(tài)調(diào)用某方法的總結(jié)(推薦)

    通過java反射機(jī)制動(dòng)態(tài)調(diào)用某方法的總結(jié)(推薦)

    下面小編就為大家?guī)硪黄ㄟ^java反射機(jī)制動(dòng)態(tài)調(diào)用某方法的總結(jié)(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-07-07
  • 為什么SpringBoot的jar可以直接運(yùn)行

    為什么SpringBoot的jar可以直接運(yùn)行

    這篇文章主要介紹了為什么SpringBoot的jar可以直接運(yùn)行,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • spring?boot?validation參數(shù)校驗(yàn)與分組嵌套各種類型及使用小結(jié)

    spring?boot?validation參數(shù)校驗(yàn)與分組嵌套各種類型及使用小結(jié)

    參數(shù)校驗(yàn)基本上是controller必做的事情,畢竟前端傳過來的一切都不可信,validation可以簡(jiǎn)化這一操作,這篇文章主要介紹了spring?boot?validation參數(shù)校驗(yàn)分組嵌套各種類型及使用小結(jié),需要的朋友可以參考下
    2023-09-09

最新評(píng)論