自学内容网 自学内容网

Java学习记录10:Stream API

Stream

Stream API把函数式编程风格引入到Java

用来针对集合进行多个数据的计算(排序,查找,过滤,映射,遍历等)

使用说明

Stream自己不会存储元素

Stream不会改变源对象,他们会返回一个持有结果的新Stream

执行流程

步骤一:Stream实例化

步骤二:一系列的中间操作

步骤三:执行终止操作

中间操作

筛选

    //筛选
    public void test04(){
        //filter(Predicate p)--接受Lambda,从流中排除某些变量
        List<Employee> list = EmployeeData.getEmployee();
        Stream<Employee> stream = list.stream();
        stream.filter(emp -> emp.getSalary() > 6000 ).forEach(System.out::println);  //filter接受一个predicate
                                                        //用lambda表达式来表示
        //foreach相当于终止操作

        //limit(n) -- 截断流,使其元素不超过给定数量
        //不能在用之前的stream了,因为一旦执行了终止操作,就不能再调用其他中间操作或终止操作了
        Stream<Employee> stream2 =  list.stream();
        stream.limit(3).forEach(System.out::println); //取前3个

        //skip(n) 跳过前n个元素
        //distinct() 根据生成元素的hashcode()和equals()去除重复元素

    }

映射

    //映射
    public void test05(){
        //map(Function f)接受一个函数,将元素转换为函数输出
        List<String> list = Arrays.asList("aa","bb","cc");
        list.stream().map(str -> str.toUpperCase()).forEach(System.out::println);

        //获取员工姓名长度大于三的员工姓名
        List<Employee> emp = EmployeeData.getEmployee();
        emp.stream().filter(e -> e.getName().length()>3).map(e -> e.getName()).
                forEach(System.out::println);
    }

排序

    //排序
    public void test06(){
    //sorted() 不传参是自然排序 传comparator是定制排序
        Integer[] arr = new Integer[]{3,2,4,1,5,32,234};
        Arrays.stream(arr).sorted().forEach(System.out::println);
    }

终止操作

匹配

@Test
    //匹配
    public void test07(){
        //allMatch(Predicate p)--检查是否匹配所有元素
        List<Employee> emp = EmployeeData.getEmployee();
        emp.stream().allMatch(e -> e.getAge()>18);
        //anyMatch(Predicate p)--检查是否至少匹配一个元素
        emp.stream().anyMatch(e -> e.getAge()>18);
        //findFirst()--返回第一个元素
        emp.stream().findFirst();
    }

查找

    @Test
    //查找
    public void test08(){
        //count -- 返回流中元素的总个数
        //max(Comparator c)--返回流中最大值
        //例:返回最高的工资
        List<Employee> e = EmployeeData.getEmployee();
        Optional s = e.stream().map(e1 -> e1.getSalary()).max((e1,e2)-> e1 - e2);
        System.out.println(s);
        //min(Comparator c)--返回流中最小值
        //forEach(Conusumer c)--内部迭代
    }

规约

    @Test
    //规约
    public void test09(){
    //reduce(T identity, BinaryOperator b)--可以将流中元素反复结合起来,得到一个值
    Arrays.asList(1,2,3,4,5,6,7,8,9,10).stream().reduce(0,(x,y)->x+y);//55
                                                                                //这边的0可以看成是一个初始值 如果是10的话这里结果就是65
    //计算员工工资总合
     List<Employee> emp = EmployeeData.getEmployee();
     System.out.println(emp.stream().map(e -> e.getSalary()).reduce(0,(x1,x2)->Integer.sum(x1,x2)));
     emp.stream().map(e -> e.getSalary()).reduce(0,Integer::sum);
    }

收集

    @Test
    //收集
    public void test10(){
    //collect(Collector c)--将流转换为其他形式。接受一个Collector接口的实现,用于给Stream中元素做汇总的方法
    List<Employee> list = EmployeeData.getEmployee();
    List<Employee> list1 = list.stream().filter(emp -> emp.getSalary() > 6000).collect(Collectors.toList());
    list1.forEach(System.out::println);

    //按照员工年龄进行排序,返回到一个新的list中
    List<Employee> list2 = EmployeeData.getEmployee();
    List<Integer> age_list = list2.stream().map(emp -> emp.getAge()).sorted(Integer::compare).collect(Collectors.toList());
    age_list.forEach(System.out::println);
    }


原文地址:https://blog.csdn.net/JTB__JJ/article/details/140692340

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!