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)文章
利用Spring Cloud Config結(jié)合Bus實(shí)現(xiàn)分布式配置中心的步驟
這篇文章主要介紹了利用Spring Cloud Config結(jié)合Bus實(shí)現(xiàn)分布式配置中心的相關(guān)資料,文中通過示例代碼將實(shí)現(xiàn)的步驟一步步介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友下面來一起看看吧2018-05-05Spring?Boot中的JdbcClient與JdbcTemplate使用對(duì)比分析
這篇文章主要介紹了Spring Boot中的JdbcClient與JdbcTemplate使用對(duì)比分析,一起看看Spring Boot 中 JdbcClient 和 JdbcTemplate 之間的差異2024-01-01SpringBoot整合Shiro實(shí)現(xiàn)登錄認(rèn)證的方法
這篇文章主要介紹了SpringBoot整合Shiro實(shí)現(xiàn)登錄認(rèn)證的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02關(guān)于QueryWrapper高級(jí)使用示例
本文介紹了QueryWrapper的高級(jí)使用方法,包括查詢指定字段、使用MySQL函數(shù)處理字段、設(shè)置查詢限制等,通過select()可查詢指定字段并處理,last()方法實(shí)現(xiàn)limit效果,apply()可在查詢條件中使用函數(shù),這些技巧有助于提升數(shù)據(jù)庫操作的靈活性和效率2024-09-09Java中g(shù)etSuperclass()方法的使用與原理解讀
文章介紹了Java中的getSuperclass()方法,該方法用于獲取一個(gè)類的直接父類,通過理解其使用方式、工作原理以及實(shí)際應(yīng)用場(chǎng)景,可以更好地利用反射機(jī)制處理類的繼承關(guān)系,實(shí)現(xiàn)動(dòng)態(tài)類型檢查、類加載以及序列化等功能2025-01-01深度源碼解析Java 線程池的實(shí)現(xiàn)原理
如何高效的使用這些資源就是程序員在平時(shí)寫代碼時(shí)候的一個(gè)努力的方向。本文要說的線程池就是一種對(duì) CPU 利用的優(yōu)化手段。對(duì)Java 線程池的實(shí)現(xiàn)原理相關(guān)知識(shí)感興趣的朋友一起看看吧2021-05-05