最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java中Stream流去除List重復(fù)元素的方法

 更新時間:2022年04月06日 16:54:00   作者:BirdMan98  
這篇文章主要為大家詳細介紹了Java中Stream流去除List重復(fù)元素的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Java中Stream流去除List重復(fù)元素的具體代碼,供大家參考,具體內(nèi)容如下

業(yè)務(wù)場景

在開發(fā)中我們常常需要過濾List中的重復(fù)對象,而重復(fù)的定義往往是根據(jù)單個條件或者多個條件,如果是單個條件的話還是比較好處理的,即使不使用工具,代碼也可以很容易實現(xiàn),但如果判斷依據(jù)不是單個條件,而是多個條件的話,代碼實現(xiàn)起來就會比較復(fù)雜,此時我們一般就會使用工具來簡化開發(fā)

單條件去重代碼

ArrayList<listData> collect = list.stream().collect(Collectors.collectingAndThen(
? ? ? ? ? Collectors.toCollection(() -> new TreeSet<>(
? ? ? ? ? ? Comparator.comparing(
? ? ? ? listData::getId))), ArrayList::new));

解釋

list-列表
listData-列表中存的對象
id是判斷是否重復(fù)的條件,只保留唯一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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

富宁县| 凤翔县| 滨州市| 蕉岭县| 长治市| 镇赉县| 通化市| 金湖县| 四子王旗| 巢湖市| 樟树市| 阳新县| 安义县| 新宁县| 普兰店市| 兴化市| 涞水县| 海南省| 大安市| 大方县| 三明市| 葵青区| 张家界市| 宝清县| 博罗县| 敦化市| 银川市| 鹤壁市| 泰来县| 柘荣县| 绵阳市| 房山区| 新沂市| 东平县| 连州市| 通州区| 化德县| 沁阳市| 英山县| 镇巴县| 疏附县|