Java中Stream流去除List重復(fù)元素的方法
本文實(shí)例為大家分享了Java中Stream流去除List重復(fù)元素的具體代碼,供大家參考,具體內(nèi)容如下
業(yè)務(wù)場(chǎng)景
在開發(fā)中我們常常需要過濾List中的重復(fù)對(duì)象,而重復(fù)的定義往往是根據(jù)單個(gè)條件或者多個(gè)條件,如果是單個(gè)條件的話還是比較好處理的,即使不使用工具,代碼也可以很容易實(shí)現(xiàn),但如果判斷依據(jù)不是單個(gè)條件,而是多個(gè)條件的話,代碼實(shí)現(xiàn)起來就會(huì)比較復(fù)雜,此時(shí)我們一般就會(huì)使用工具來簡化開發(fā)
單條件去重代碼
ArrayList<listData> collect = list.stream().collect(Collectors.collectingAndThen( ? ? ? ? ? Collectors.toCollection(() -> new TreeSet<>( ? ? ? ? ? ? Comparator.comparing( ? ? ? ? listData::getId))), ArrayList::new));
解釋
list-列表
listData-列表中存的對(duì)象
id是判斷是否重復(fù)的條件,只保留唯一id對(duì)象
多條件去重代碼
ArrayList<listData> collect = list.stream().collect(Collectors.collectingAndThen( ? ? ?Collectors.toCollection(() -> new TreeSet<>( ? ? ? ?Comparator.comparing(p->p.getPatentName() + ";" + p.getLevel()))), ArrayList::new));
測(cè)試代碼
import java.util.*; import java.util.stream.Collectors; public class ExcelUtil { ? ? private static String[] params = {"p001","p002","p003","p004"}; ? ? public static void main(String[] args) { ? ? ? ? List<Datum> dataList = new ArrayList<>(); ? ? ? ? for (int i = 0; i < 100; i++) { ? ? ? ? ? ? if (i%2==0){ ? ? ? ? ? ? ? ? Datum datum = new Datum( ? ? ? ? ? ? ? ? ? ? ? ? params[new Random().nextInt(params.length)], ? ? ? ? ? ? ? ? ? ? ? ? params[new Random().nextInt(params.length)], ? ? ? ? ? ? ? ? ? ? ? ? params[new Random().nextInt(params.length)], ? ? ? ? ? ? ? ? ? ? ? ? params[new Random().nextInt(params.length)], ? ? ? ? ? ? ? ? ? ? ? ? params[new Random().nextInt(params.length)] ? ? ? ? ? ? ? ? ); ? ? ? ? ? ? ? ? dataList.add(datum); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ?? ? ? ? ? System.out.println("0 size : "+dataList.size()+" -> "+dataList); ? ? ? ? // 單條件 ? ? ? ? ArrayList<Datum> collect1 = dataList.stream().collect(Collectors.collectingAndThen( ? ? ? ? ? ? ? ? Collectors.toCollection(() -> new TreeSet<Datum>( ? ? ? ? ? ? ? ? ? ? ? ? Comparator.comparing( ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Datum::getId))), ArrayList::new)); ? ? ? ? System.out.println("1 size : "+collect1.size()+" -> "+collect1); ? ? ? ? // 兩個(gè)條件 ? ? ? ? ArrayList<Datum> collect2 = dataList.stream().collect(Collectors.collectingAndThen( ? ? ? ? ? ? ? ? Collectors.toCollection(() -> new TreeSet<>( ? ? ? ? ? ? ? ? ? ? ? ? Comparator.comparing(p->p.getId() + ";" + p.getAddress()))), ArrayList::new)); ? ? ? ? System.out.println("2 size : "+collect2.size()+" -> "+collect2); ? ? ? ? // 三個(gè)條件 ? ? ? ? ArrayList<Datum> collect3 = dataList.stream().collect(Collectors.collectingAndThen( ? ? ? ? ? ? ? ? Collectors.toCollection(() -> new TreeSet<>( ? ? ? ? ? ? ? ? ? ? ? ? Comparator.comparing(p->p.getInfo() + ";" + p.getAddress()+";"+p.getName()))), ArrayList::new)); ? ? ? ? System.out.println("3 size : "+collect3.size()+" -> "+collect3); ? ? } }
效果
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA 如何導(dǎo)入別人的javaweb項(xiàng)目進(jìn)行部署
這篇文章主要介紹了IDEA 如何導(dǎo)入別人的javaweb項(xiàng)目進(jìn)行部署,本文給大家分享我的詳細(xì)部署過程及遇到問題解決方法,需要的朋友可以參考下2023-03-03java 實(shí)現(xiàn)比較版本號(hào)功能
本篇文章主要介紹了java 中涉及到客戶端的系統(tǒng)經(jīng)常需要用到比較版本號(hào)的功能,并附小示例,希望能幫助需要的小伙伴2016-07-07教你使用eclipse?搭建Swt?環(huán)境的全過程
本文給大家分享使用eclipse?搭建Swt?環(huán)境的全過程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12Mybatis空值關(guān)聯(lián)的問題解析及解決方案
這篇文章給大家介紹了Mybatis空值關(guān)聯(lián)的問題解析及解決方案,文中通過代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01SpringCloud Eureka自我保護(hù)機(jī)制原理解析
這篇文章主要介紹了SpringCloud Eureka自我保護(hù)機(jī)制原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02SpringSecurity跨域請(qǐng)求偽造(CSRF)的防護(hù)實(shí)現(xiàn)
本文主要介紹了SpringSecurity跨域請(qǐng)求偽造(CSRF)的防護(hù)實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07