JAVA8获取list集合中重复的元素与获取去重数据实例
更新时间:2023年07月05日 11:06:57 作者:胡萝卜★
这篇文章主要给大家介绍了关于JAVA8获取list集合中重复的元素与获取去重数据的相关资料,在实际开发中经常会遇到需要找出(删除)一个list中某些元素的属性相同的元素,需要的朋友可以参考下
1.java8获取list集合中重复的元素
//单独String集合
List<String> list = Arrays.asList("a","b","a","c","d","b");
List<String> collect = list.stream().filter(i -> i != "") // list 对应的 Stream 并过滤""
.collect(Collectors.toMap(e -> e, e -> 1, Integer::sum)) // 获得元素出现频率的 Map,键为元素,值为元素出现的次数
.entrySet()
.stream() // 所有 entry 对应的 Stream
.filter(e -> e.getValue() > 1) // 过滤出元素出现次数大于 1 (重复元素)的 entry
.map(Map.Entry::getKey) // 获得 entry 的键(重复元素)对应的 Stream
.collect(Collectors.toList());
System.out.println(collect);2.java8根据List对象属性获取重复数据和获取去重后数据
2.1获取重复数据
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("张三", 8, 3000));
personList.add(new Person("李四", 18, 5000));
personList.add(new Person("王五", 28, 7000));
personList.add(new Person("孙六", 38, 9000));
personList.add(new Person("孙六", 38, 9000));
personList.add(new Person("孙六", 38, 10000));
//1.先根据得到一个属于集合 姓名
List<String> uniqueList = personList.stream().collect(Collectors.groupingBy(Person::getName, Collectors.counting()))
.entrySet().stream().filter(e -> e.getValue() > 1)
.map(Map.Entry::getKey).collect(Collectors.toList());
uniqueList.forEach(p -> System.out.println(p));
//计算两个list中的重复值 3条数据
List<Person> reduce1 = personList.stream().filter(item -> uniqueList.contains(item.getName())).collect(Collectors.toList());
System.out.println(reduce1.toString());
//2.根据多个属性获取重复数据,只能重复操作得到重复的数据 工资
List<Integer> collect1 = reduce1.stream().collect(Collectors.groupingBy(Person::getWages, Collectors.counting()))
.entrySet().stream().filter(e -> e.getValue() > 1)
.map(Map.Entry::getKey).collect(Collectors.toList());
//计算两个list中的差集
List<Person> collect2 = reduce1.stream().filter(item -> collect1.contains(item.getWages())).collect(Collectors.toList());
System.out.println(collect2.toString());结果: 根据多个属性获取重复数据,还在摸索中,欢迎大家来指点!!!!!

2.2获取去重后数据
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("张三", 8, 3000));
personList.add(new Person("李四", 18, 5000));
personList.add(new Person("王五", 28, 7000));
personList.add(new Person("孙六", 38, 9000));
personList.add(new Person("孙六", 38, 9000));
personList.add(new Person("孙六", 38, 10000));
//去重
List<Person> unique = personList.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName))), ArrayList::new));
System.out.println("unique:"+unique.toString());结果:

总结
到此这篇关于JAVA8获取list集合中重复的元素与获取去重数据的文章就介绍到这了,更多相关JAVA8获取list集合重复元素内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
springboot上传zip包并解压至服务器nginx目录方式
这篇文章主要介绍了springboot上传zip包并解压至服务器nginx目录方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2025-04-04
Springboot 2.x中server.servlet.context-path的运用详解
这篇文章主要介绍了Springboot 2.x中server.servlet.context-path的运用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2021-01-01
string boot 与 自定义interceptor的实例讲解
下面小编就为大家分享一篇string boot 与 自定义interceptor的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2017-12-12
Spring Security 2026 构建安全、可靠的企业应用实践指南
文章概述了SpringSecurity2026的核心特性、认证与授权的最佳实践、安全防护和会话管理措施,以及在微服务架构中的应用,并探讨了未来的安全趋势,强调通过合理配置和实践构建更安全的企业应用2026-04-04
如何解决idea的Translation插件google翻译无法使用问题
这篇文章主要介绍了如何解决idea的Translation插件google翻译无法使用问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-05-05


最新评论