java流式计算学习笔记

导读:本篇文章讲解 java流式计算学习笔记,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

Java Stream流式计算:Java 8 API添加了一个新的抽象称为流Stream,可以让你以一种声明的方式处理数据。

Stream API可以极大提高Java程序员的生产力,让程序员写出高效率、干净、简洁的代码。
这种风格将要处理的元素集合看作一种流, 流在管道中传输, 并且可以在管道的节点上进行处理, 比如筛选, 排序,聚合等。
元素流在管道中经过中间操作(intermediate operation)的处理,最后由最终操作(terminal operation)得到前面处理的结果。

请添加图片描述

以上的流程转换为 Java 代码为:

List<Integer> transactionsIds = 
widgets.stream()
             .filter(b -> b.getColor() == RED)
             .sorted((x,y) -> x.getWeight() - y.getWeight())
             .mapToInt(Widget::getWeight)
             .sum();

flatMap(对流扁平化处理)

使用flatMap方法的效果是,各个数组并不是分别映射一个流,而是映射成流的内容,所有使用map(Array::stream)时生成的单个流被合并起来,即扁平化为一个流。

Company类:

package 任务十三__流式计算;


import java.util.List;

/**
 * @author ${范涛之}
 * @Description
 * @create 2021-12-01 21:11
 */
public class Company<T> {
    public  enum Type {BIG,SMALL};

    private String name ;
    private Type type;
    private List<T> employees;

    public Company(String name, Type type, List<T> employees) {
        this.name = name;
        this.type = type;
        this.employees = employees;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }
    public List<T> getEmployees() {
        return employees;
    }
    public void setEmployees(List<T> employees) {
        this.employees = employees;
    }
    @Override
    public String toString() {
     return  this.name;
    }


}

Employee类:

package 任务十三__流式计算;

/**
 * @author ${范涛之}
 * @Description
 * @create 2021-12-01 21:18
 */
public class Employee {
    public enum Type {MANAGER, SELLER, OFFICER};
    private String name;
    private String genger;
    private Integer age;
    private boolean married;
    private Type type;

    public Employee(String name, String genger, Integer age, boolean married, Type type) {
        super();
        this.name = name;
        this.genger = genger;
        this.age = age;
        this.married = married;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGenger() {
        return genger;
    }

    public void setGenger(String genger) {
        this.genger = genger;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public boolean isMarried() {
        return married;
    }

    public void setMarried(boolean married) {
        this.married = married;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Employee{");
        sb.append("name='").append(name).append('\'');
        sb.append(", genger='").append(genger).append('\'');
        sb.append(", age=").append(age);
        sb.append(", married=").append(married);
        sb.append(", type=").append(type);
        sb.append('}');
        return sb.toString();
    }
}

测试类:

package 任务十三__流式计算;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ${范涛之}
 * @Description
 * @create 2021-12-01 21:24
 */
public class Test {
    public static void main(String[] args) {
        List<Company<Employee>> list = new ArrayList<>();
        List<Employee> employees = Arrays.asList(
                new Employee("张勇", "男", 28, true, Employee.Type.MANAGER),
                new Employee("李强", "男", 22, false, Employee.Type.SELLER),
                new Employee("王武", "男", 32, false, Employee.Type.SELLER),
                new Employee("梅丽", "女", 26, true, Employee.Type.OFFICER),
                new Employee("郑帅", "男", 29, false, Employee.Type.OFFICER),
                new Employee("曾美", "女", 27, true, Employee.Type.SELLER),
                new Employee("郝俊", "男", 22, true, Employee.Type.SELLER),
                new Employee("方圆", "女", 24, false, Employee.Type.SELLER)
        );
        Company<Employee> moubao = new Company<Employee>("某宝", Company.Type.BIG, employees);
        employees = Arrays.asList(

                new Employee("吴琼", "女", 27, true, Employee.Type.SELLER),
                new Employee("陈辰", "女", 20, false, Employee.Type.OFFICER),
                new Employee("刘能", "男", 25, true, Employee.Type.OFFICER),
                new Employee("周七", "男", 29, false, Employee.Type.OFFICER),
                new Employee("汪旺", "男", 21, false, Employee.Type.OFFICER),
                new Employee("胡涂", "男", 27, false, Employee.Type.OFFICER),
                new Employee("杨茂", "男", 34, true, Employee.Type.MANAGER),
                new Employee("朱坚", "男", 30, false, Employee.Type.MANAGER)
        );
        Company<Employee> mouxin = new Company<>("某东", Company.Type.BIG, employees);

        employees = Arrays.asList(

                new Employee("冯过", "男", 35, false, Employee.Type.SELLER),
                new Employee("何花", "女", 27, false, Employee.Type.MANAGER),
                new Employee("卫精", "男", 25, true, Employee.Type.OFFICER),
                new Employee("施工", "男", 28, false, Employee.Type.OFFICER),
                new Employee("沈月", "女", 24, false, Employee.Type.OFFICER),
                new Employee("乐欢", "女", 22, false, Employee.Type.OFFICER),
                new Employee("安全", "男", 33, true, Employee.Type.MANAGER),
                new Employee("林森", "男", 26, true, Employee.Type.SELLER)
        );
        Company<Employee> mouhaha = new Company<>("某哈哈", Company.Type.SMALL, employees);

        list.add(moubao);
        list.add(mouxin);
        list.add(mouhaha);





    }

}


然后开始分析需求写流:统计公司类型为 BIG 的所有未婚员工,同时按年龄排序,并且未婚员工不区分公司,也就是最终结果放在一个列表中

首先1:返回的是筛选后的员工,所以我们要定义一个数组用来存放返回的结果:

      List<Employee> result = list.stream

2:然后开始筛选:首先使用flatMap合并流,然后根据条件筛选,


     List<Employee> result = list.stream()
             .flatMap(company -> company.getEmployees().stream())
             .filter(employee -> !employee.isMarried())
             .sorted(Comparator.comparing(Employee::getAge))
             .collect(Collectors.toList());
     for (Employee emp :result){
         System.out.println(emp.toString());
     }

最后打印输出:

请添加图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/81037.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!