JAVA8獲取list集合中重復(fù)的元素與獲取去重?cái)?shù)據(jù)實(shí)例
1.java8獲取list集合中重復(fù)的元素
//單獨(dú)String集合
List<String> list = Arrays.asList("a","b","a","c","d","b");
List<String> collect = list.stream().filter(i -> i != "") // list 對(duì)應(yīng)的 Stream 并過濾""
.collect(Collectors.toMap(e -> e, e -> 1, Integer::sum)) // 獲得元素出現(xiàn)頻率的 Map,鍵為元素,值為元素出現(xiàn)的次數(shù)
.entrySet()
.stream() // 所有 entry 對(duì)應(yīng)的 Stream
.filter(e -> e.getValue() > 1) // 過濾出元素出現(xiàn)次數(shù)大于 1 (重復(fù)元素)的 entry
.map(Map.Entry::getKey) // 獲得 entry 的鍵(重復(fù)元素)對(duì)應(yīng)的 Stream
.collect(Collectors.toList());
System.out.println(collect);2.java8根據(jù)List對(duì)象屬性獲取重復(fù)數(shù)據(jù)和獲取去重后數(shù)據(jù)
2.1獲取重復(fù)數(shù)據(jù)
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.先根據(jù)得到一個(gè)屬于集合 姓名
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));
//計(jì)算兩個(gè)list中的重復(fù)值 3條數(shù)據(jù)
List<Person> reduce1 = personList.stream().filter(item -> uniqueList.contains(item.getName())).collect(Collectors.toList());
System.out.println(reduce1.toString());
//2.根據(jù)多個(gè)屬性獲取重復(fù)數(shù)據(jù),只能重復(fù)操作得到重復(fù)的數(shù)據(jù) 工資
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());
//計(jì)算兩個(gè)list中的差集
List<Person> collect2 = reduce1.stream().filter(item -> collect1.contains(item.getWages())).collect(Collectors.toList());
System.out.println(collect2.toString());結(jié)果: 根據(jù)多個(gè)屬性獲取重復(fù)數(shù)據(jù),還在摸索中,歡迎大家來指點(diǎn)?。。。。?/strong>

2.2獲取去重后數(shù)據(jù)
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());結(jié)果:

總結(jié)
到此這篇關(guān)于JAVA8獲取list集合中重復(fù)的元素與獲取去重?cái)?shù)據(jù)的文章就介紹到這了,更多相關(guān)JAVA8獲取list集合重復(fù)元素內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Apollo是如何實(shí)現(xiàn)配置更新的
這篇文章主要介紹了Java Apollo是如何實(shí)現(xiàn)配置更新的,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-03-03
springboot上傳zip包并解壓至服務(wù)器nginx目錄方式
這篇文章主要介紹了springboot上傳zip包并解壓至服務(wù)器nginx目錄方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
Springboot 2.x中server.servlet.context-path的運(yùn)用詳解
這篇文章主要介紹了Springboot 2.x中server.servlet.context-path的運(yùn)用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
string boot 與 自定義interceptor的實(shí)例講解
下面小編就為大家分享一篇string boot 與 自定義interceptor的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
java新特性之for循環(huán)最全的用法總結(jié)
下面小編就為大家?guī)硪黄猨ava新特性之for循環(huán)最全的用法總結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12
Spring?Security?2026?構(gòu)建安全、可靠的企業(yè)應(yīng)用實(shí)踐指南
文章概述了SpringSecurity2026的核心特性、認(rèn)證與授權(quán)的最佳實(shí)踐、安全防護(hù)和會(huì)話管理措施,以及在微服務(wù)架構(gòu)中的應(yīng)用,并探討了未來的安全趨勢(shì),強(qiáng)調(diào)通過合理配置和實(shí)踐構(gòu)建更安全的企業(yè)應(yīng)用2026-04-04
spring使用validation參數(shù)及全局異常檢測(cè)方式
本文主要介紹了Java的數(shù)據(jù)校驗(yàn)規(guī)范validation-api,包括其定義的注解和HibernateValidator的實(shí)現(xiàn),還介紹了spring-boot-starter-validation的使用,可以讓開發(fā)者在SpringBoot應(yīng)用中簡(jiǎn)化數(shù)據(jù)校驗(yàn)的功能2025-02-02
如何解決idea的Translation插件google翻譯無法使用問題
這篇文章主要介紹了如何解決idea的Translation插件google翻譯無法使用問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
idea設(shè)置JVM運(yùn)行參數(shù)的幾種方式
對(duì)JVM運(yùn)行參數(shù)進(jìn)行修改是JVM性能調(diào)優(yōu)的重要手段,本文主要介紹了idea設(shè)置JVM運(yùn)行參數(shù)的幾種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04

