1、String
集合去重,例如List<String>
@Test
public void listDistinctByStreamDistinct() {
//数据准备
List<String> strList = new ArrayList<String>() {{
add("A");
add("A");
add("B");
add("B");
add("C");
}};
//控制台打印原始数据
System.out.println("去重前:");
for (String s : strList) {
System.out.println(s);
}
List<String> collectList = strList.stream().distinct().collect(Collectors.toList());
//控制台打印原始数据
System.out.println("去重后:");
for (String s : collectList) {
System.out.println(s);
}
}
//控制台日志如下:
/*
去重前:AABBC
去重后:ABC
*/
2、实体对象集合去重,例如List<Student>
/**
* 定义一个实体类
*/
@Data
public class Student {
private String stuNo;
private String name;
}
@Test
public void listDistinctByStreamDistinct() throws Exception {
//假设List<Student>数据已经准备好了,命名为studentList
System.out.println("去重前:/n");
System.out.println(studentList);
List<Student> coList = studentList.stream().distinct().collect(Collectors.toList());
System.out.println("去重后:/n");
System.out.println(coList);
}
//控制台日志
/*
去重前:
[
{"stuNo":"001","name":"Tom"},
{"stuNo":"002","name":"Mike"},
{"stuNo":"001","name":"Tom"}
]
去重后:
[
{"stuNo":"001","name":"Tom"},
{"stuNo":"002","name":"Mike"}
]
*/
3. 根据 List<Object>
中 Object
的某个属性去重
/filter去重方法实现类
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 distinctByProperty1() throws Exception {
System.out.println("去重前:/n");
System.out.println(studentList);
//根据对象去重
List<Student> coList = studentList.stream().distinct().collect(Collectors.toList());
System.out.println("去重后:/n");
System.out.println(coList);
//根据对象的属性去重1
List<Student> nList1 = studentList.stream().collect(collectingAndThen(
toCollection(() -> new
TreeSet<(Comparator.comparing(Student::getName))),
ArrayList::new));
//根据对象的属性去重2
List<Student> nList2 = studentList.stream().filter(distinctByKey(Student::getName))
.collect(Collectors.toList());
System.out.println("方法1根据name属性去重后:/n");
System.out.println(nList1);
System.out.println("方法2根据name属性去重后:/n");
System.out.println(nList2);
}
//控制台打印
/*
去重前:
[
{"stuNo":"001","name":"Tom"},
{"stuNo":"001","name":"Tom"},
{"stuNo":"003","name":"Tom"}
]
去重后:
[
{"stuNo":"001","name":"Tom"},
{"stuNo":"003","name":"Tom"}
]
方法1根据name属性去重后:
[
{"stuNo":"001","name":"Tom"}
]
方法2根据name属性去重后:
[
{"stuNo":"001","name":"Tom"}
]
*/