Java中Stream流去除List重復元素的方法
本文實例為大家分享了Java中Stream流去除List重復元素的具體代碼,供大家參考,具體內容如下
業(yè)務場景
在開發(fā)中我們常常需要過濾List中的重復對象,而重復的定義往往是根據單個條件或者多個條件,如果是單個條件的話還是比較好處理的,即使不使用工具,代碼也可以很容易實現(xiàn),但如果判斷依據不是單個條件,而是多個條件的話,代碼實現(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);
? ? }
}效果

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Scheduled如何會在上次任務執(zhí)行完才會執(zhí)行下次任務
這篇文章主要介紹了Scheduled如何會在上次任務執(zhí)行完才會執(zhí)行下次任務問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
IDEA安裝阿里巴巴編碼規(guī)范插件的兩種方式詳解(在線安裝和離線安裝)
這篇文章主要介紹了IDEA安裝阿里巴巴編碼規(guī)范插件的兩種方式詳解(在線安裝和離線安裝),本文通過截圖給大家展示的非常詳細,需要的朋友可以參考下2021-09-09
SpringBoot實現(xiàn)登錄攔截器超詳細教程分享
對于管理系統(tǒng)或其他需要用戶登錄的系統(tǒng),登錄驗證都是必不可少的環(huán)節(jié),尤其在?SpringBoot?開發(fā)的項目中。本文為大家準備了超詳細的SpringBoot實現(xiàn)登錄攔截器方法,快收藏一波吧2023-02-02

