在這里我來分享幾種列表去重的方法,算是一次整理吧,如有紕漏,請不吝賜教。1. Stream 的distinct()方法
distinct()
是Java 8 中 Stream 提供的方法,返回的是由該流中不同元素組成的流。distinct()
使用 hashCode()
和 eqauls()
方法來獲取不同的元素。
因此,需要去重的類必須實現(xiàn) hashCode()
和 equals()
方法。換句話講,我們可以通過重寫定制的 hashCode()
和 equals() 方法來達(dá)到某些特殊需求的去重。
distinct()
方法聲明如下:
Stream<T> distinct();
“如果您正在學(xué)習(xí)Spring Boot,那么推薦一個連載多年還在繼續(xù)更新的免費教程:http://blog./spring-boot-learning-2x/
”
1.1 對于 String 列表的去重
因為 String 類已經(jīng)覆寫了 equals()
和 hashCode()
方法,所以可以去重成功。
@Test
public void listDistinctByStreamDistinct() {
// 1. 對于 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 對于實體類列表的去重
注:代碼中我們使用了Lombok 插件的 @Data
注解,可自動覆寫 equals()
以及 hashCode()
方法。
/**
* 定義一個實體類
*/
@Data
public class Student {
private String stuNo;
private String name;
}
@Test
public void listDistinctByStreamDistinct() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
// 1. 對于 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"}]
2. 根據(jù) List<Object>
中 Object 某個屬性去重
2.1 新建一個列表出來
@Test
public void distinctByProperty1() throws JsonProcessingException {
// 這里第一種方法我們通過新創(chuàng)建一個只有不同元素列表來實現(xiàn)根據(jù)對象某個屬性去重
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));
// 這里我們引入了兩個靜態(tài)方法,以及通過 TreeSet<> 來達(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 通過 filter() 方法
我們首先創(chuàng)建一個方法作為 Stream.filter()
的參數(shù),其返回類型為 Predicate,原理就是判斷一個元素能否加入到 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 {
// 這里第二種方法我們通過過濾來實現(xiàn)根據(jù)對象某個屬性去重
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ù),過濾掉那些不能加入到 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"}]
3. 總結(jié)
以上便是我要分享的幾種關(guān)于列表去重的方法,當(dāng)然這里沒有進(jìn)行更為詳盡的性能分析,希望以后會深入底層再重新分析一下。如有紕漏,還望不吝賜教。
來源:https:///post/68449038
42132262926
ps:如果您覺文章有用,動動小手點個在看,點個贊再走吧