需求:根据学生名称去重,获取新的集合
@Data
public class Student {
private String stuNo;
private String name;
}
//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 {
//根据对象的属性去重
List<Student> nList2 = studentList.stream().filter(distinctByKey(Student::getName))
.collect(Collectors.toList());
}