判断是否有重复数据,有重复不加到map中
private 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;
}
将数据存入List,再用list调用java的stream() api 的过滤方法filter(),将上面的方法传入,方法中传实体的字段,可实现根据字段过滤重复数据
List<entry>list=new List<>
List<?> distinct = list.stream().filter(distinctByKey(entry::get***)).collect(Collectors.toList());