List<RewardCycleVo> result = list.stream()
.filter(distinctByKey((p) ->
(p.getId()))).collect(Collectors.toList());
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;
}
//putIfAbsent 如果传入key对应的value已经存在,就返回存在的value,不进行替换。
//如果不存在,就添加key和value,返回null
集合根据对象属性去重
最新推荐文章于 2022-10-26 09:45:28 发布