广告
返回顶部
首页 > 资讯 > 后端开发 > Python >java使用stream判断两个list元素的属性并输出方式
  • 934
分享到

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

2024-04-02 19:04:59 934人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录使用stream判断两个list元素的属性并输出stream判断列表是否包含某几个元素/重复元素代码SHOWJava stream判断列表是否包含重复元素使用stream判断两个

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


@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;
}

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。 

--结束END--

本文标题: java使用stream判断两个list元素的属性并输出方式

本文链接: https://www.lsjlt.com/news/150800.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作