标签:reac 根据 ring ted Collector col equals 生产力 strong
Stream是Java8提供的一个新的API,它位于java.util.stream包下。Stream API提供了一种新的方式来对Java集合进行操作,这种操作方式极大的提高了Java程序员的生产力,让程序员写出高效率、干净、简洁的代码。
我们可以将元素集合看作一种流, 流在管道中传输, 并且可以在管道的节点上进行处理, 比如筛选, 排序,聚合等。元素流在管道中经过中间操作的处理,最后由最终操作得到前面处理的结果
class Employee {
private Long empno; //员工号
private String ename; //员工姓名
private Integer salary; //薪水
private Integer deptno; //所属部门号
//此处省略get/set方法、构造方法以及toString方法
}
Employee e1 = new Employee(7369L, "SMITH", 800, 20);
Employee e2 = new Employee(7499L, "ALLEN", 1600, 30);
Employee e3 = new Employee(7521L, "WARD", 1250, 30);
Employee e4 = new Employee(7782L, "CLARK", 2450, 10);
Employee e5 = new Employee(7876L, "ADAMS", 1100, 20);
List<Employee> employees = Arrays.asList(e1, e2, e3, e4, e5);
employees.stream().forEach(System.out::println);
执行结果:
Employee{empno=7369, ename=‘SMITH‘, salary=800, deptno=20}
Employee{empno=7499, ename=‘ALLEN‘, salary=1600, deptno=30}
Employee{empno=7521, ename=‘WARD‘, salary=1250, deptno=30}
Employee{empno=7782, ename=‘CLARK‘, salary=2450, deptno=10}
Employee{empno=7876, ename=‘ADAMS‘, salary=1100, deptno=20}
//获取所有员工的姓名
List<String> enames = employees.stream().map(employee -> employee.getEname()).collect(Collectors.toList());
enames.stream().forEach(System.out::println);
执行结果:
SMITH
ALLEN
WARD
CLARK
ADAMS
//获取所有员工的薪水总和
int totalSalary = employees.stream().mapToInt(employee -> employee.getSalary()).sum();
System.out.println("薪水总和:" + totalSalary);
执行结果:
薪水总和:7200
//获取薪水超过1500的员工
List<Employee> filterEmp = employees.stream().filter(employee -> employee.getSalary()>1500).collect(Collectors.toList());
filterEmp.stream().forEach(System.out::println);
执行结果:
Employee{empno=7499, ename=‘ALLEN‘, salary=1600, deptno=30}
Employee{empno=7782, ename=‘CLARK‘, salary=2450, deptno=10}
//按员工的薪水由低到高排序
List<Employee> sortedEmp = employees.stream().sorted(Comparator.comparing(Employee::getSalary)).collect(Collectors.toList());
sortedEmp.stream().forEach(System.out::println);
执行结果:
Employee{empno=7369, ename=‘SMITH‘, salary=800, deptno=20}
Employee{empno=7876, ename=‘ADAMS‘, salary=1100, deptno=20}
Employee{empno=7521, ename=‘WARD‘, salary=1250, deptno=30}
Employee{empno=7499, ename=‘ALLEN‘, salary=1600, deptno=30}
Employee{empno=7782, ename=‘CLARK‘, salary=2450, deptno=10}
//按员工所属部门号进行分类
Map<Integer, List<Employee>> map = employees.stream().collect(Collectors.groupingBy(employee -> employee.getDeptno()));
for(Map.Entry<Integer, List<Employee>> entry : map.entrySet()) {
System.out.println("key: " + entry.getKey() + " value:" + entry.getValue());
}
System.out.println();
//获取员工姓名,用","进行拼接
String enameString = employees.stream().map(employee -> employee.getEname()).collect(Collectors.joining(","));
System.out.println(enameString);
执行结果:
key: 20 value:[Employee{empno=7369, ename=‘SMITH‘, salary=800, deptno=20}, Employee{empno=7876, ename=‘ADAMS‘, salary=1100, deptno=20}]
key: 10 value:[Employee{empno=7782, ename=‘CLARK‘, salary=2450, deptno=10}]
key: 30 value:[Employee{empno=7499, ename=‘ALLEN‘, salary=1600, deptno=30}, Employee{empno=7521, ename=‘WARD‘, salary=1250, deptno=30}]
SMITH,ALLEN,WARD,CLARK,ADAMS
//获取20号部门员工姓名,按薪水从高到低排序
List<String> names = employees.stream().filter(employee -> employee.getDeptno().equals(20)).sorted(Comparator.comparing(Employee::getSalary).reversed()).map(employee -> employee.getEname()).collect(Collectors.toList());
names.stream().forEach(System.out::println);
执行结果:
ADAMS
SMITH
标签:reac 根据 ring ted Collector col equals 生产力 strong
原文地址:https://www.cnblogs.com/baobeiqi-e/p/10096929.html