private void book(List<Item> books){
List<Integer> bookIds = books.stream().filter(distinctByKey(m ->m.getBookId())).map(m -> m.getBookId()).collect(Collectors.toList());
}
//根据属性去重 distinct
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;
}
jdk8 stream 根据属性去重
最新推荐文章于 2024-01-19 15:49:32 发布