参考
总结
1,distinct去重原理:类实现hashCode()和equals()方法
2,filter根据属性过滤去重:支持单属性与多重属性去重。
不足:多重属性去重必须提前已知属性个数。
核心代码:
// 去重predict方法
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor){
Map<Object,Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
// 单属性去重 按照age去重
students.stream().filter(distinctByKey(Student::getAge)).forEach(System.out::println);
/** 输出 age不同,有三个
* Student{name='张三', age=18, type=1}
* Student{name='李四', age=20, type=1}
* Student{name='小明', age=22, type=3}
*/
System.out.println("---------cut line------------");
// 多属性去重 按照name+age+type去重
students.stream().filter(distinctByKey(Student::getName)).filter(distinctByKey(Student::getAge))
.filter(distinctByKey(Student::getType)).forEach(System.out::println);
/** 输出 name+age+type不同,有2个
* Student{name='张三', age=18, type=1}
* Student{name='小明', age=22, type=3}
*/
}
//根据多个实体属性去重
List<entity> list3 = list.stream().filter(distinctByKey((p) -> (p.get属性名称1()))).filter(distinctByKey((p) -> (p.get属性名称2()))).collect(Collectors.toList());
3,TreeSet去重:定义Comparator比较器
核心代码:
// --------单属性去重 按照age去重---------
TreeSet<Student> treeSet = students.stream().collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getAge))));
treeSet.forEach(System.out::println);
/** 输出 age不同的,有三个
* Student{name='张三', age=18, type=1}
* Student{name='李四', age=20, type=1}
* Student{name='小明', age=22, type=3}
*/
System.out.println("-----------cut line--------");
// 想把结果再转化成List,可以使用 collectingAndThen()方法 该方法先获取数据,然后再对数据进行操作
List<Student> unique = students.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getAge))), ArrayList::new)
);
unique.forEach(System.out::println);
/** 输出 age不同的,有三个
* Student{name='张三', age=18, type=1}
* Student{name='李四', age=20, type=1}
* Student{name='小明', age=22, type=3}
*/
System.out.println("-----------cut line--------");
// --------多属性去重 按照name+age+type去重--------
List<Student> unique1 = students.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getName() + "-" + o.getAge() + "-" + o.getType()))), ArrayList::new)
);
unique1.forEach(System.out::println);
/** 输出 name+age+type都不同的,有四个
* Student{name='Jack', age=22, type=3}
* Student{name='小明', age=22, type=3}
* Student{name='张三', age=18, type=1}
* Student{name='李四', age=20, type=1}
*/
比较器写法
@Test
public void test(){
// 第一种
Comparator<Book> comparator = (o1, o2) -> o1.getPrice() - o2.getPrice();
int compare = comparator.compare(new Book("Java", 20.0), new Book("C++", 20.0));
// 第二种
Comparator<Book> comparing = Comparator.comparing(book -> book.getPrice());
int compare2 = comparing.compare(new Book("Java", 20.0), new Book("C++", 20.0));
// 输出是结果一样的
System.out.println("compare = " + compare);
System.out.println("compara2 = " + compare2);
}