• stream流的使用


    stream流的使用

     @org.junit.Test
        //Stream API代替for循环
        //过滤字母并且排序
        public void test1() {
            List<String> nameStrs = Arrays.asList("Monkey", "Lion", "Giraffe", "Lemur");
            /*
             *   sorted() 默认自然排序(根据0-9或者a-z)
             */
            List<String> list = nameStrs.stream()
                    .filter(s -> s.startsWith("L"))
                    .map(String::toUpperCase)
                    .sorted()
                    .collect(toList());
            System.out.println(list);
            //结果   [LEMUR, LION]
    
        }
    
        @org.junit.Test
        //使用流的过滤
        //filter传入了lambda表达式
        public void test2() {
            Employee e1 = new Employee(1, 23, "M", "Rick", "Beethovan");
            Employee e2 = new Employee(2, 13, "F", "Martina", "Hengis");
            Employee e3 = new Employee(3, 43, "M", "Ricky", "Martin");
            Employee e4 = new Employee(4, 26, "M", "Jon", "Lowman");
          
            List<Employee> employees = Arrays.asList(e1, e2, e3, e4);
            //.filter(e -> e.getAge() > 70 && e.getGender().equals("M"))也可以如下写
            //Predicate<Employee > predicate = user -> user.getAge() > 70&& user.getGender().equals("M");
            //.filter(predicate)
            List<Employee> filtered = employees.stream()
                    .filter(e -> e.getAge() > 70 && e.getGender().equals("M"))
                    .collect(Collectors.toList());
            System.out.println(employees);
            System.out.println("------------------------");
            System.out.println(filtered);
        }
    
        @org.junit.Test
        //Stream管道流的map操作
        //所以map函数的作用就是针对管道流中的每一个数据元素进行转换操作。
        public void test3() {
            List<String> alpha = Arrays.asList("Monkey", "Lion", "Giraffe", "Lemur");
    
            //不使用Stream管道流
            List<String> alphaUpper = new ArrayList<>();
            for (String s : alpha) {
                alphaUpper.add(s.toUpperCase());
            }
            System.out.println(alphaUpper); //[MONKEY, LION, GIRAFFE, LEMUR]
    
            // 使用Stream管道流
            List<String> collect = alpha.stream().map(String::toUpperCase).collect(Collectors.toList());
            //上面使用了方法引用,和下面的lambda表达式语法效果是一样的
            //List<String> collect = alpha.stream().map(s -> s.toUpperCase()).collect(Collectors.toList());
    
            System.out.println(collect); //[MONKEY, LION, GIRAFFE, LEMUR]
    
        }
        @org.junit.Test
        //map()函数不仅可以处理数据,还可以转换数据的类型
        public void test4(){
            Employee e1 = new Employee(1,23,"M","Rick","Beethovan");
            Employee e2 = new Employee(2,13,"F","Martina","Hengis");
            Employee e3 = new Employee(3,43,"M","Ricky","Martin");
            Employee e4 = new Employee(4,26,"M","Jon","Lowman");
    
            List<Employee> employees = Arrays.asList(e1, e2, e3, e4);
    
        /*List<Employee> maped = employees.stream()
                .map(e -> {
                    e.setAge(e.getAge() + 1);
                    //三元表达式
                    e.setGender(e.getGender().equals("M")?"male":"female");
                    //这里一定要加return
                    return e;
                }).collect(Collectors.toList());*/
            //由于map的参数e就是返回值,所以可以用peek函数。
            // peek函数是一种特殊的map函数,当函数没有返回值或者参数就是返回值的时候可以使用peek函数。
            List<Employee> maped = employees.stream()
                    .peek(e -> {
                        e.setAge(e.getAge() + 1);
                        e.setGender(e.getGender().equals("M")?"male":"female");
                    }).collect(Collectors.toList());
            System.out.println(maped);
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88

    排序

    @org.junit.Test
        public void test6() {
            List<Integer> list = Arrays.asList(1,3,2,5,6,4);
            //取最大值
            System.out.println(list.stream().max(Integer::compareTo).get());
            //取最小值
            System.out.println(list.stream().min(Integer::compareTo).get());
            //降序排列
            list.stream().sorted(Comparator.reverseOrder()).forEach(x -> {
                System.out.println(x);
            });
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  • 相关阅读:
    thinkphp在apache、nginx和iis下的URL重写
    计算大于2的任意正整数n以内的所有素数(质数)的和
    Django性能之道:缓存应用与优化实战
    扫雷 | C语言 | 简单易懂 | 扫雷相关知识点总结
    微信在线点餐怎么做_怎么实现在微信公众号在线点餐
    Linux时间同步练习
    【BUG】Nginx转发失败解决方案
    2203 CSDN课程-python入门课
    听说:分布式ID不能全局递增?
    点云从入门到精通技术详解100篇-基于点云数据的机器人动态分拣
  • 原文地址:https://blog.csdn.net/qq_44892120/article/details/125488923