首页 > 其他分享 >Stream通过某个字段过滤重复元素

Stream通过某个字段过滤重复元素

时间:2022-11-04 15:24:59浏览次数:37  
标签:distinctByKey apple Stream 过滤 list add Book 某个 new

List<Book> list = new ArrayList<>();
        list.add(new Book("apple"));
        list.add(new Book("apple"));
        list.add(new Book("pear"));
        list.add(new Book("melon"));

        List<Book> assetBasicInfoEntityList = list.stream()
                .filter(distinctByKey(Book::getName))
                .collect(Collectors.toList());

 

private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Set<Object> seen = ConcurrentHashMap.newKeySet();
        return t -> seen.add(keyExtractor.apply(t));
    }

 

标签:distinctByKey,apple,Stream,过滤,list,add,Book,某个,new
From: https://www.cnblogs.com/juniorMa/p/16857894.html

相关文章