Java8 常用stream流使用(后续用到会持续更新)

一、使用stream流过滤

        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }
        System.out.println(list);

        List<Integer> collect1 = list.stream().filter(item -> item > 2).collect(Collectors.toList());
        System.out.println(collect1); // [3, 4, 5, 6, 7, 8, 9]
        List<Integer> collect2 = list.stream().filter(item -> item.equals(2)).collect(Collectors.toList());
        System.out.println(collect2); // [2]
        List<Integer> collect3 = list.stream().filter(item -> item.equals(2) || item.equals(3)).collect(Collectors.toList());
        System.out.println(collect3); //[2, 3]

二、使用stream流分组

        List<Pair<Integer, String>> pairArrayList = new ArrayList<>();
        pairArrayList.add(new Pair<>(1, "a"));
        pairArrayList.add(new Pair<>(1, "b"));
        pairArrayList.add(new Pair<>(1, "c"));
        pairArrayList.add(new Pair<>(2, "d"));
        pairArrayList.add(new Pair<>(2, "e"));
        pairArrayList.add(new Pair<>(2, "f"));
        // stream流分组
        Map<Integer, List<Pair<Integer, String>>> collect = pairArrayList.stream().collect(Collectors.groupingBy(item -> item.getKey()));
        System.out.println(collect); // {1=[1=a, 1=b, 1=c], 2=[2=d, 2=e, 2=f]}

        // stream流分组统计个数
        Map<Integer, Long> map = pairArrayList.stream().collect(Collectors.groupingBy(item -> item.getKey(), Collectors.counting()));
        System.out.println(map); // {1=3, 2=3}

三、使用stream流排序

        List<Student> list = new ArrayList<>();
        list.add(new Student(2, "a", 12));
        list.add(new Student(4, "b", 15));
        list.add(new Student(6, "c", 54));
        list.add(new Student(7, "d", 5));
        list.add(new Student(2, "w", 23));

        // 按id排序升序
        List<Student> collect = list.stream().sorted(Comparator.comparing(Student :: getId)).collect(Collectors.toList());
        collect.forEach(e -> System.out.println("Id:" + e.getId() + ", Name: " + e.getName() + ", Age:" + e.getAge()));
        //Id:2, Name: a, Age:12
        //Id:2, Name: w, Age:23
        //Id:4, Name: b, Age:15
        //Id:6, Name: c, Age:54
        //Id:7, Name: d, Age:5

        // 按id排序倒序
        List<Student> collect2 = list.stream().sorted(Comparator.comparing(Student :: getId).reversed()).collect(Collectors.toList());
        collect2.forEach(e -> System.out.println("Id:" + e.getId() + ", Name: " + e.getName() + ", Age:" + e.getAge()));
        //Id:7, Name: d, Age:5
        //Id:7, Name: d, Age:5
        //Id:6, Name: c, Age:54
        //Id:4, Name: b, Age:15
        //Id:2, Name: a, Age:12
        //Id:2, Name: w, Age:23

        // 如果id一样则二次通过年龄排序
        List<Student> collect3 = list.stream().sorted(Comparator.comparing(Student :: getId).thenComparing(Student :: getAge).reversed()).collect(Collectors.toList());
        collect3.forEach(e -> System.out.println("Id:" + e.getId() + ", Name: " + e.getName() + ", Age:" + e.getAge()));
        //Id:7, Name: d, Age:5
        //Id:6, Name: c, Age:54
        //Id:4, Name: b, Age:15
        //Id:2, Name: w, Age:23
        //Id:2, Name: a, Age:12

四、使用stream流去重

        List<Student> list = new ArrayList<>();
        list.add(new Student(2, "a", 12));
        list.add(new Student(4, "b", 15));
        list.add(new Student(6, "c", 54));
        list.add(new Student(7, "d", 5));
        list.add(new Student(2, "w", 23));

        // 获取学生的id并去重
        Set<Integer> collect = list.stream().map(item -> item.getId()).collect(Collectors.toSet());
        System.out.println(collect);

        // 根据学生id去重
        List<Student> students = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getId))), ArrayList::new));
        students.forEach(e -> System.out.println("Id:" + e.getId() + ", Name: " + e.getName() + ", Age:" + e.getAge()));
        //Id:2, Name: a, Age:12
        //Id:4, Name: b, Age:15
        //Id:6, Name: c, Age:54
        //Id:7, Name: d, Age:5

五、使用stream流过滤分组汇总计算

        List<Student> list = new ArrayList<>();
        list.add(new Student(2, "a", 12));
        list.add(new Student(4, "b", 15));
        list.add(new Student(4, "c", 54));
        list.add(new Student(2, "d", 5));
        list.add(new Student(2, "w", 23));

        // 查询id相同的学生的平均年龄
        Map<Integer, Double> map = list.stream().collect(
                // 按id分组
                Collectors.groupingBy(item -> item.getId(),
                        // 统计平均年龄
                        Collectors.averagingDouble(item -> item.getAge())));
        map.forEach((k,v) -> System.out.println("key:" + k + "value" + v));
        //key:2value13.333333333333334
        //key:4value34.5

六、总结

filter方法(中间节点):过滤

distinct方法(中间节点):去重

limit方法(中间节点):截取

skip方法(中间节点):跳过

map方法(中间节点):映射,多用于数据的转换

flatMap方法(中间节点):合并流

       List<String> words = Arrays.asList("Hello","World");
       List<String> list = words.stream()
               .map(word -> word.split(""))
               .flatMap(Arrays::stream)
               .distinct()
               .collect(toList());

       System.out.println(list);  // [H, e, l, o, W, r, d]

anyMatch方法(终端节点):只要有一个元素匹配返回true

allMatch方法(终端节点):只要有一个元素不匹配返回false

noneMatch方法(终端节点):只要有一个元素匹配返回false

        List<String> list = new ArrayList<>();
        list.add("香蕉");
        list.add("苹果");
        list.add("梨");
        list.add("西瓜");
        list.add("葡萄");

        boolean anyMatch = list.stream().anyMatch(item -> item.contains("西")); //true
        boolean allMatch = list.stream().allMatch(item -> item.length() > 1); //false
        boolean noneMatch = list.stream().noneMatch(item -> item.contains("东")); //true

forEach方法(终端节点):遍历

collect方法(终端节点):转换流

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值