Java之stream流去重,单字段,多字段都可

1.封装工具类

public class DeduplicationUtil {
    public 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;
    }
}

2.简易学生对象

//偷懒用了lombok,自动生成getter和setter以及构造函数
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
    private String id_;
    private String name_;
}

3.使用junit测试

public class DemoTest {
    @Test
    public void test(){
        Student student1 = new Student("1","aaa");
        Student student2 = new Student("1","bbb");
        Student student3 = new Student("1","ccc");
        Student student4 = new Student("2","ccc");
        ArrayList<Student> list = new ArrayList<>();
        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.add(student4);
        //使用封装方法的多字段
        List<Student> collect = list.stream()
                .filter(
                        DeduplicationUtil.distinctByKey(s -> s.getId_() + ";" + s.getName_())
                ).collect(Collectors.toList());
        System.out.println(collect);

        //多字段TreeSet
        ArrayList<Student> collect1 = list.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(
                        Comparator.comparing(s->s.getId_() + ";" + s.getName_()))), ArrayList::new));
        System.out.println(collect1);
    }
}

单字段只要get一个不拼接即可,和distinct()方法区别是不需要重写hashcode以及equal方法,这样可以避免一些问题因为这些去重不完整。

参考文章:

https://blog.csdn.net/Kurry4ever_/article/details/109638367
https://www.jianshu.com/p/b1362370c77d
https://haiyoung.blog.csdn.net/article/details/80934467
https://blog.csdn.net/yojofly/article/details/100986216

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值