Java中Stream流去除List重復元素的方法
本文實例為大家分享了Java中Stream流去除List重復元素的具體代碼,供大家參考,具體內(nèi)容如下
業(yè)務場景
在開發(fā)中我們常常需要過濾List中的重復對象,而重復的定義往往是根據(jù)單個條件或者多個條件,如果是單個條件的話還是比較好處理的,即使不使用工具,代碼也可以很容易實現(xiàn),但如果判斷依據(jù)不是單個條件,而是多個條件的話,代碼實現(xiàn)起來就會比較復雜,此時我們一般就會使用工具來簡化開發(fā)
單條件去重代碼
ArrayList<listData> collect = list.stream().collect(Collectors.collectingAndThen( ? ? ? ? ? Collectors.toCollection(() -> new TreeSet<>( ? ? ? ? ? ? Comparator.comparing( ? ? ? ? listData::getId))), ArrayList::new));
解釋
list-列表
listData-列表中存的對象
id是判斷是否重復的條件,只保留唯一id對象
多條件去重代碼
ArrayList<listData> collect = list.stream().collect(Collectors.collectingAndThen( ? ? ?Collectors.toCollection(() -> new TreeSet<>( ? ? ? ?Comparator.comparing(p->p.getPatentName() + ";" + p.getLevel()))), ArrayList::new));
測試代碼
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); ? ? ? ? // 兩個條件 ? ? ? ? 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); ? ? ? ? // 三個條件 ? ? ? ? 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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
教你使用eclipse?搭建Swt?環(huán)境的全過程
本文給大家分享使用eclipse?搭建Swt?環(huán)境的全過程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12SpringSecurity跨域請求偽造(CSRF)的防護實現(xiàn)
本文主要介紹了SpringSecurity跨域請求偽造(CSRF)的防護實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07