使用java8实现List中对象属性的去重

作者: 李玉凯 分类: JAVA 发布时间: 2020-12-28 15:09

单个属性去重

ArrayList<BookInfoVo> distinctLiost = list.stream()
                .collect(
                        Collectors.collectingAndThen(
                                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparingLong(BookInfoVo::getRecordId))), ArrayList::new));

多个属性去重

ArrayList<BookInfoVo> distinctLiost = list.stream()
                .collect(
                        Collectors.collectingAndThen(
                                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(
                       o -> o.getName() + ";" + o.getAuthor()))), ArrayList::new));

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

邮箱地址不会被公开。 必填项已用*标注

− 5 = 3