语法

/**
* 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);

teeing的作用

它是一个新的静态方法,用于 java.util.stream.Collectors 接口,它允许使用两个独立的收集器进行收集,然后使用提供的 BiFunction 合并它们的结果。

传递给结果收集器的每个元素都由两个下游收集器处理,然后使用指定的合并函数将它们的结果合并到最终结果中。

请注意,此功能有助于在单个步骤中执行特定任务。
如果我们不使用 teeing()函数,我们已经可以分两步执行给定的任务。
它只是一个帮助函数,有助于减少冗长。

使用 teeing() 过滤项目并统计它们

在此示例中,我们将使用同一组员工。
在这里,我们会找到所有工资大于 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);
	}
}
Java 12 Collectors.teeing() 方法
on  it road.com

使用 teeing() 查找最高和最低工资的员工

在这个 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);
	}
}

输出:

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 + "]";
	}
}
日期:2020-09-17 00:09:31 来源:oir作者:oir