【stream】jdk1.8中stream流的第n+1种用法,去除重复list集合

需求:若获取的list集合有重叠,则去除重复的集合,然后展示

思考:若是判断重复对象,可以重写对象的equales方法进行判断,list集合则用.distinct()即可实现

思路:先把所有的集合放在一起,然后使用stream的distinct()方法去重

示例:
public static List<Test1> ss() {
        List<Test1> instruments = new ArrayList<>();
        instruments.add(new Test1(0.09, "3"));
        instruments.add(new Test1(0.28, "3"));
        instruments.add(new Test1(0.03, "3"));
        instruments.add(new Test1(0.3, "3"));
        instruments.add(new Test1(0.09, "3"));
        instruments.add(new Test1(0.1, "3"));
        instruments.add(new Test1(0.3, "3"));
        instruments.add(new Test1(0.4, "3"));
        if (CollectionUtils.isNotEmpty(instruments)) {
            List<Test1> listAll = new ArrayList<>();
            List<Test1> supportListTwoSection = instruments.stream().sorted(Comparator.comparing(Test1::getNearThreeBsDelta)).collect(Collectors.toList()).subList(0,1);
            List<Test1> supportListThreeSection = instruments.stream().sorted(Comparator.comparing(Test1::getNearBsDelta)).collect(Collectors.toList()).subList(0,1);
            List<Test1> supportListOneSection = instruments.stream().sorted(Comparator.comparing(Test1::getNearTwoBsDelta)).collect(Collectors.toList()).subList(0,1);


            supportListTwoSection.addAll(supportListThreeSection);
            supportListTwoSection.addAll(supportListOneSection);

            supportListTwoSection = supportListTwoSection.stream().sorted(Comparator.comparing(Test1::getId)).distinct().collect(Collectors.toList());
            System.out.println(supportListTwoSection);
            return supportListTwoSection;
        }else{
            return Collections.emptyList();
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值