Java8利用Stream實(shí)現(xiàn)列表去重的方法詳解
一. Stream 的distinct()方法
distinct()是Java 8 中 Stream 提供的方法,返回的是由該流中不同元素組成的流。distinct()使用 hashCode() 和 eqauls() 方法來(lái)獲取不同的元素。
因此,需要去重的類必須實(shí)現(xiàn) hashCode() 和 equals() 方法。換句話講,我們可以通過(guò)重寫(xiě)定制的 hashCode() 和 equals() 方法來(lái)達(dá)到某些特殊需求的去重。
distinct() 方法聲明如下:
Stream<T> distinct();
1.1 對(duì)于 String 列表的去重
因?yàn)?String 類已經(jīng)覆寫(xiě)了 equals() 和 hashCode() 方法,所以可以去重成功。
@Test
public void listDistinctByStreamDistinct() {
// 1. 對(duì)于 String 列表去重
List<String> stringList = new ArrayList<String>() {{
add("A");
add("A");
add("B");
add("B");
add("C");
}};
out.print("去重前:");
for (String s : stringList) {
out.print(s);
}
out.println();
stringList = stringList.stream().distinct().collect(Collectors.toList());
out.print("去重后:");
for (String s : stringList) {
out.print(s);
}
out.println();
}結(jié)果如下:
去重前:AABBC
去重后:ABC
1.2 對(duì)于實(shí)體類列表的去重
注:代碼中我們使用了 Lombok 插件的 @Data注解,可自動(dòng)覆寫(xiě) equals() 以及 hashCode() 方法。
/**
* 定義一個(gè)實(shí)體類
*/
@Data
public class Student {
private String stuNo;
private String name;
}@Test
public void listDistinctByStreamDistinct() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
// 1. 對(duì)于 Student 列表去重
List<Student> studentList = getStudentList();
out.print("去重前:");
out.println(objectMapper.writeValueAsString(studentList));
studentList = studentList.stream().distinct().collect(Collectors.toList());
out.print("去重后:");
out.println(objectMapper.writeValueAsString(studentList));
}結(jié)果如下:
去重前:[{"stuNo":"001","name":"Tom"},{"stuNo":"002","name":"Mike"},{"stuNo":"001","name":"Tom"}]
去重后:[{"stuNo":"001","name":"Tom"},{"stuNo":"002","name":"Mike"}]
二. 根據(jù) List<Object> 中 Object 某個(gè)屬性去重
2.1 新建一個(gè)列表出來(lái)
@Test
public void distinctByProperty1() throws JsonProcessingException {
// 這里第一種方法我們通過(guò)新創(chuàng)建一個(gè)只有不同元素列表來(lái)實(shí)現(xiàn)根據(jù)對(duì)象某個(gè)屬性去重
ObjectMapper objectMapper = new ObjectMapper();
List<Student> studentList = getStudentList();
out.print("去重前 :");
out.println(objectMapper.writeValueAsString(studentList));
studentList = studentList.stream().distinct().collect(Collectors.toList());
out.print("distinct去重后:");
out.println(objectMapper.writeValueAsString(studentList));
// 這里我們引入了兩個(gè)靜態(tài)方法,以及通過(guò) TreeSet<> 來(lái)達(dá)到獲取不同元素的效果
// 1. import static java.util.stream.Collectors.collectingAndThen;
// 2. import static java.util.stream.Collectors.toCollection;
studentList = studentList.stream().collect(
collectingAndThen(
toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getName))), ArrayList::new)
);
out.print("根據(jù)名字去重后 :");
out.println(objectMapper.writeValueAsString(studentList));
}結(jié)果如下:
去重前 :[{"stuNo":"001","name":"Tom"},{"stuNo":"001","name":"Tom"},{"stuNo":"003","name":"Tom"}]
distinct去重后:[{"stuNo":"001","name":"Tom"},{"stuNo":"003","name":"Tom"}]
根據(jù)名字去重后 :[{"stuNo":"001","name":"Tom"}]
2.2 通過(guò) filter() 方法
我們首先創(chuàng)建一個(gè)方法作為 Stream.filter() 的參數(shù),其返回類型為 Predicate,原理就是判斷一個(gè)元素能否加入到 Set 中去,代碼如下:
private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Set<Object> seen = ConcurrentHashMap.newKeySet();
return t -> seen.add(keyExtractor.apply(t));
}使用如下:
@Test
public void distinctByProperty2() throws JsonProcessingException {
// 這里第二種方法我們通過(guò)過(guò)濾來(lái)實(shí)現(xiàn)根據(jù)對(duì)象某個(gè)屬性去重
ObjectMapper objectMapper = new ObjectMapper();
List<Student> studentList = getStudentList();
out.print("去重前 :");
out.println(objectMapper.writeValueAsString(studentList));
studentList = studentList.stream().distinct().collect(Collectors.toList());
out.print("distinct去重后:");
out.println(objectMapper.writeValueAsString(studentList));
// 這里我們將 distinctByKey() 方法作為 filter() 的參數(shù),過(guò)濾掉那些不能加入到 set 的元素
studentList = studentList.stream().filter(distinctByKey(Student::getName)).collect(Collectors.toList());
out.print("根據(jù)名字去重后 :");
out.println(objectMapper.writeValueAsString(studentList));
}結(jié)果如下:
去重前 :[{"stuNo":"001","name":"Tom"},{"stuNo":"001","name":"Tom"},{"stuNo":"003","name":"Tom"}]
distinct去重后:[{"stuNo":"001","name":"Tom"},{"stuNo":"003","name":"Tom"}]
根據(jù)名字去重后 :[{"stuNo":"001","name":"Tom"}]
到此這篇關(guān)于Java8利用Stream實(shí)現(xiàn)列表去重的方法詳解的文章就介紹到這了,更多相關(guān)Java8 列表去重內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Sentinel熱門詞匯限流的實(shí)現(xiàn)詳解
這篇文章主要介紹了使用Sentinel對(duì)熱門詞匯進(jìn)行限流的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Springboot接收前端的Json但是為null問(wèn)題
這篇文章主要介紹了Springboot接收前端的Json但是為null問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教<BR>2024-05-05
如何使用jenkins實(shí)現(xiàn)發(fā)布部分更新文件
這篇文章主要介紹了如何使用jenkins實(shí)現(xiàn)發(fā)布部分更新文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
IntelliJ IDEA(2019)安裝破解及HelloWorld案例(圖文)
這篇文章主要介紹了IntelliJ IDEA(2019)安裝破解及HelloWorld案例(圖文),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Java連接PostgreSql數(shù)據(jù)庫(kù)及基本使用方式
這篇文章主要介紹了Java連接PostgreSql數(shù)據(jù)庫(kù)及基本使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Spring裝配Bean之用Java代碼安裝配置bean詳解
這篇文章主要給大家介紹了關(guān)于Spring裝配Bean之用Java代碼安裝配置bean的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
java實(shí)現(xiàn)超大文件的讀寫(xiě)功能
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)超大文件的讀寫(xiě)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
Java 模擬數(shù)據(jù)庫(kù)連接池的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java 模擬數(shù)據(jù)庫(kù)連接池的實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
springboot集成spring cache緩存示例代碼
本篇文章主要介紹了springboot集成spring cache示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05

