java使用stream判断两个list元素的属性并输出方式

作者:enjoy嚣士 时间:2023-02-06 14:15:18 

使用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. 判断list的size大小(intCheck>1找到两个则代表同时出现)

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.");
        }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。 

来源:https://enjoy-day.blog.csdn.net/article/details/116456140

标签:stream,list元素,属性,输出
0
投稿

猜你喜欢

  • Android实现Tab布局的4种方式(Fragment+TabPageIndicator+ViewPager)

    2023-02-26 10:45:41
  • JAVA 根据设置的概率生成随机数的方法

    2023-07-15 00:13:37
  • Android 实现代码混淆的实例

    2023-06-01 22:13:48
  • Java实现五子棋的基础方法

    2021-07-11 12:32:08
  • 用Kotlin实现Android点击事件的方法

    2023-01-16 15:56:03
  • c#自带缓存使用方法 c#移除清理缓存

    2021-09-07 10:20:15
  • Java杂谈之重复代码是什么

    2022-06-08 15:07:24
  • 如何使用正则表达式判断邮箱(以C#为例)

    2022-12-09 21:38:30
  • 详谈Java中的Object、T(泛型)、?区别

    2022-06-11 21:13:11
  • Springboot打包为Docker镜像并部署的实现

    2023-06-05 23:11:52
  • java GUI实现学生图书管理简单实例

    2023-11-11 05:00:05
  • 2020年IntelliJ IDEA最新最详细配置图文教程详解

    2022-08-19 00:07:51
  • JAVA Spring中让人头痛的JAVA大事务问题要如何解决你知道吗

    2023-01-14 04:58:04
  • Java数据结构之链表详解

    2023-10-20 09:14:08
  • SpringCloud2020整合Nacos-Bootstrap配置不生效的解决

    2023-11-29 02:42:47
  • Android5.0新控件实例详解

    2022-12-06 21:58:18
  • java并发编程专题(三)----详解线程的同步

    2022-03-18 05:35:42
  • MyBatisPlus 自定义sql语句的实现

    2021-11-11 07:51:20
  • Java实现二叉树的深度优先遍历和广度优先遍历算法示例

    2021-07-16 12:37:17
  • Java代理模式实例详解【静态代理与动态代理】

    2023-04-27 08:18:51
  • asp之家 软件编程 m.aspxhome.com