收集器teeing()方法示例

收集器teeing()方法示例

原文: https://howtodoinjava.com/java12/collectors-teeing-example/

了解Collectors.teeing()方法(在 Java 12 中添加),方法语法以及如何在 Java 的各种用例中应用teeing()方法。

1. teeing()的目的

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

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

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

2. 语法

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

3. 使用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 + "]";
	}
}

4. 使用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);
	}
}

程序输出:

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]]
}

5. 总结

上面的Collectors.teeing()方法示例非常简单,为便于基本理解而编写。 您需要使用非常适合您自己需要的函数。

只需记住,当您需要执行两次流操作并在两个不同的收集器中收集结果时,请考虑使用teeing()方法。 它并不总是适合用例,但适合时可能会有用。

学习愉快!

参考: Java 文档