Java8 Stream distinct 去重

文章目录

参考

Stream distinct 去重

总结

1,distinct去重原理:类实现hashCode()和equals()方法

2,filter根据属性过滤去重:支持单属性与多重属性去重。
不足:多重属性去重必须提前已知属性个数。
核心代码:

// 去重predict方法
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;
}

// 单属性去重 按照age去重
        students.stream().filter(distinctByKey(Student::getAge)).forEach(System.out::println);
        /** 输出 age不同,有三个
         * Student{name='张三', age=18, type=1}
         * Student{name='李四', age=20, type=1}
         * Student{name='小明', age=22, type=3}
         */

        System.out.println("---------cut line------------");

        // 多属性去重 按照name+age+type去重
        students.stream().filter(distinctByKey(Student::getName)).filter(distinctByKey(Student::getAge))
                .filter(distinctByKey(Student::getType)).forEach(System.out::println);
        /** 输出 name+age+type不同,有2个
         * Student{name='张三', age=18, type=1}
         * Student{name='小明', age=22, type=3}
         */
    }


//根据多个实体属性去重
List<entity> list3 = list.stream().filter(distinctByKey((p) -> (p.get属性名称1()))).filter(distinctByKey((p) -> (p.get属性名称2()))).collect(Collectors.toList());

3,TreeSet去重:定义Comparator比较器
核心代码:

// --------单属性去重 按照age去重---------
        TreeSet<Student> treeSet = students.stream().collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getAge))));
        treeSet.forEach(System.out::println);
        /** 输出 age不同的,有三个
         * Student{name='张三', age=18, type=1}
         * Student{name='李四', age=20, type=1}
         * Student{name='小明', age=22, type=3}
         */
        System.out.println("-----------cut line--------");

        // 想把结果再转化成List,可以使用 collectingAndThen()方法 该方法先获取数据,然后再对数据进行操作
        List<Student> unique = students.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getAge))), ArrayList::new)
        );
        unique.forEach(System.out::println);
        /** 输出 age不同的,有三个
         * Student{name='张三', age=18, type=1}
         * Student{name='李四', age=20, type=1}
         * Student{name='小明', age=22, type=3}
         */

        System.out.println("-----------cut line--------");

        // --------多属性去重 按照name+age+type去重--------
        List<Student> unique1 = students.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getName() + "-" + o.getAge() + "-" + o.getType()))), ArrayList::new)
        );
        unique1.forEach(System.out::println);
        /** 输出 name+age+type都不同的,有四个
         * Student{name='Jack', age=22, type=3}
         * Student{name='小明', age=22, type=3}
         * Student{name='张三', age=18, type=1}
         * Student{name='李四', age=20, type=1}
         */

比较器写法

@Test
public void test(){
    // 第一种
    Comparator<Book> comparator = (o1, o2) -> o1.getPrice() - o2.getPrice();
    int compare = comparator.compare(new Book("Java", 20.0), new Book("C++", 20.0));
    
    // 第二种
    Comparator<Book> comparing = Comparator.comparing(book -> book.getPrice());
    int compare2 = comparing.compare(new Book("Java", 20.0), new Book("C++", 20.0));

    // 输出是结果一样的
    System.out.println("compare = " + compare);
    System.out.println("compara2 = " + compare2);
}
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值