0
点赞
收藏
分享

微信扫一扫

JAVA8 Stream List中元素根据元素属性进行去重

不会弹吉他的二郎腿 2022-01-31 阅读 102
listjava
// 创建一个包含DeptEntity对象的List,并向其中添加若干元素
List<DeptEntity> deptEntityList= new ArrayList<>();
deptEntityList.add(new DeptEntity(1, '部门1'));
deptEntityList.add(new DeptEntity(1, '部门1'));
deptEntityList.add(new DeptEntity(2, '部门2'));

// 使用java8的stream流进行元素去重(根据DeptEntity中的id进行元素去重)
ArrayList<DeptEntity> disList = deptEntityList.stream().collect(
        Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(DeptEntity::getId))), ArrayList::new))
举报

相关推荐

0 条评论