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

 更新时间:2022年06月07日 16:48:08   作者:enjoy嚣士  
这篇文章主要介绍了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. 判断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.");
        }

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

相关文章

  • springBoot @Enable* 注解的使用

    springBoot @Enable* 注解的使用

    这篇文章主要介绍了springBoot @Enable* 注解的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • java枚举如何使用spring的@value注入属性

    java枚举如何使用spring的@value注入属性

    这篇文章主要介绍了java枚举如何使用spring的@value注入属性问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • springMvc请求的跳转和传值的方法

    springMvc请求的跳转和传值的方法

    本篇文章主要介绍了springMvc请求的跳转和传值的方法,这里整理了几种跳转方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-02-02
  • Java中Synchronized的用法解析

    Java中Synchronized的用法解析

    synchronized是Java中的关键字,是一种同步锁,本文给大家详细介绍Java Synchronized 用法大全,感兴趣的朋友跟随小编一起看看吧
    2021-11-11
  • 详谈Springfox与swagger的整合使用

    详谈Springfox与swagger的整合使用

    下面小编就为大家带来一篇详谈Springfox与swagger的整合使用。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • spring5新特性全面介绍

    spring5新特性全面介绍

    本文将全面介绍 Spring 5。我将介绍 Java SE 8 和 Java EE 7 API 的基准升级、Spring 5 的新反应式编程模型、HTTP/2 支持,以及 Spring 通过 Kotlin 对函数式编程的全面支持。
    2020-10-10
  • 一文读懂JavaWeb前后端数据交互

    一文读懂JavaWeb前后端数据交互

    本文主要介绍了一文读懂JavaWeb前后端数据交互,包括HTTP请求与响应、JSON数据格式以及常用的数据传输技术,具有一定的参考价值,感兴趣的可以了解一下
    2024-01-01
  • 重写equals的同时为何要重写hashCode?

    重写equals的同时为何要重写hashCode?

    这篇文章主要给大家介绍了关于重写equals的同时为何要重写hashCode的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • Java实现将html字符串插入到PPT幻灯片

    Java实现将html字符串插入到PPT幻灯片

    Java后端代码操作PPT幻灯片时,可直接在幻灯片中绘制形状,并在形状中添加文本字符串内容。本篇文章主要介绍通过java实现将html字符串添加到PPT幻灯片的的方法,可添加文字、图片、视频、音频等。以下是具体方法和步骤。
    2021-11-11
  • java创建二维码并赋予url链接的功能实现

    java创建二维码并赋予url链接的功能实现

    这篇文章给大家分享java创建二维码并赋予url链接的功能实现,需要获取要赋值给二维码的链接后缀,通过设置二维码的访问路径等一系列操作,具体实现代码跟随小编一起看看吧
    2021-06-06

最新评论