方式1
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;
}
使用方法:用Stream接口的 filter()接收为参数
List<Entity> lists = list.stream().filter(distinctByKey(b->b.getTid())).collect(Collectors.toList());
方式2
存在重复数据的问题,这里使用stream流的衍生功能,去除一个对象中的部分元素的重复如下:
ArrayList<PatentDto> collect1 = patentDtoList.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(
Comparator.comparing(p->p.getPatentName() + ";" + p.getLevel()))), ArrayList::new)