stream问题清单

根据对象的某些属性去重

//PageData是自定义的map的子类
public List<PageData> getAllGoods() {
        List<PageData> pageDataList = goodsDao.selectAllGoods();
        //使用map去重
        List<PageData> unique2 = pageDataList.stream()
                .filter(distinctByKey(o -> o.get("id")))
                .collect(Collectors.toList());
       // System.out.println(unique2);
        return unique2;
    }

    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        //System.out.println("这个函数将应用到每一个item");
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

将对象列表List< Object >转Map

List转Map的三种方法

发布了437 篇原创文章 · 获赞 82 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/qq_41063141/article/details/103918283
今日推荐