了解 Collectors.teeing()方法(Java 12中已添加),方法语法以及如何在Java的各种用例中应用 teeing()
方法。
这是一种新的静态方法,用于加入 java.util.stream.Collectors 接口,该接口允许使用两个独立的收集器进行收集,然后使用提供的 BiFunction 合并其结果。
传递给结果收集器的每个元素均由两个下游收集器处理,然后使用指定的合并函数将其结果合并为最终结果。
请注意,此函数有助于一步完成特定任务。如果不使用teeing()
函数,无涯教程已经可以分两步执行给定的任务。它只是一个帮助函数,可以帮助减少冗长程度。
/** * downstream1 - the first downstream collector * downstream2 - the second downstream collector * merger - the function which merges two results into the single one * returns - a Collector which aggregates the results of two supplied collectors. */ public static Collector teeing (Collector downstream1, Collector downstream2, BiFunction merger);
在此 Collectors.teeing()示例中,有一个员工列表。想一步找到最高薪的员工和最低薪的员工。
以下Java程序执行查找最大和最小操作,然后将两个项目收集到Map中。
import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.HashMap; import java.util.Optional; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<Employee> employeeList = Arrays.asList( new Employee(1, "A", 100), new Employee(2, "B", 200), new Employee(3, "C", 300), new Employee(4, "D", 400)); HashMap<String, Employee> result = employeeList.stream().collect( Collectors.teeing( Collectors.maxBy(Comparator.comparing(Employee::getSalary)), Collectors.minBy(Comparator.comparing(Employee::getSalary)), (e1, e2) -> { HashMap<String, Employee> map = new HashMap(); map.put("MAX", e1.get()); map.put("MIN", e2.get()); return map; } )); System.out.println(result); } }
程序输出。
链接:https://www.learnfk.comhttps://www.learnfk.com/java/java12-collectors-teeing-example.html
来源:LearnFk无涯教程网
C:\BAML\DFCCUI\installs\jdk-12.0.1\bin>java Main.java { MIN=Employee [id=1, name=A, salary=100.0], MAX=Employee [id=4, name=D, salary=400.0] }
这里的 Employee
类是这样的。
class Employee { private long id; private String name; private double salary; public Employee(long id, String name, double salary) { super(); this.id = id; this.name = name; this.salary = salary; } //Getters and setters @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + "]"; } }
在此示例中,无涯教程将使用同一组员工。在这里将找到所有薪水高于200的员工,然后还将计算这些员工的数量。
import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.HashMap; import java.util.Optional; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<Employee> employeeList = Arrays.asList( new Employee(1, "A", 100), new Employee(2, "B", 200), new Employee(3, "C", 300), new Employee(4, "D", 400)); HashMap<String, Object> result = employeeList.stream().collect( Collectors.teeing( Collectors.filtering(e -> e.getSalary() > 200, Collectors.toList()), Collectors.filtering(e -> e.getSalary() > 200, Collectors.counting()), (list, count) -> { HashMap<String, Object> map = new HashMap(); map.put("list", list); map.put("count", count); return map; } )); System.out.println(result); } }
程序输出。
链接:https://www.learnfk.comhttps://www.learnfk.com/java/java12-collectors-teeing-example.html
来源:LearnFk无涯教程网
C:\BAML\DFCCUI\installs\jdk-12.0.1\bin>java Main.java { count=2, list=[Employee [id=3, name=C, salary=300.0], Employee [id=4, name=D, salary=400.0]] }
上面的Collectors.teeing()方法示例非常简单,为基本理解而编写。您需要使用非常适合您自己需要的函数。
只需记住,当您需要执行两次流操作并在两个不同的收集器中收集结果时,请考虑使用 teeing()方法。参考: Java文档
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)