根据对象的某些属性去重
public List<PageData> getAllGoods() {
List<PageData> pageDataList = goodsDao.selectAllGoods();
List<PageData> unique2 = pageDataList.stream()
.filter(distinctByKey(o -> o.get("id")))
.collect(Collectors.toList());
return unique2;
}
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
将对象列表List< Object >转Map
List转Map的三种方法