Stream API可以极大提高Java程序员的生产力,让程序员写出高效率、干净、简洁的代码。
这种风格将要处理的元素集合看作一种流, 流在管道中传输, 并且可以在管道的节点上进行处理, 比如筛选, 排序,聚合等。
元素流在管道中经过中间操作(intermediate operation)的处理,最后由最终操作(terminal operation)得到前面处理的结果。
List<Integer> transactionsIds =
widgets.stream()
.filter(b -> b.getColor() == RED)
.sorted((x,y) -> x.getWeight() - y.getWeight())
.mapToInt(Widget::getWeight)
.sum();
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;
}
}
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);
}
}
List<Employee> result = list.stream
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://blog.csdn.net/justleavel/article/details/121657472
内容来源于网络,如有侵权,请联系作者删除!