- Java 12 Tutorial
- Java 12 - Home
- Java 12 - Overview
- Java 12 - Environment Setup
- Java 12 - Switch Expressions
- Java 12 - File mismatch method
- Java 12 - Compact Number Formatting
- Java 12 - Teeing Collectors
- Java 12 - String methods
- Garbage Collection Enhancements
- Java 12 - Microbenchmark
- Java Tutorial
- Java 8 Tutorial
- Java 9 Tutorial
- Java 10 Tutorial
- Java 11 Tutorial
- Java 12 Useful Resources
- Java 12 - Quick Guide
- Java 12 - Useful Resources
- Java 12 - Discussion
Java 12 - Tee 收集器
Java 12 向 Collectors 引入了一种新方法,可以对集合执行两种不同的操作,然后合并结果。以下是 teeing 方法的语法 -
Collector<T, ?, R> teeing( Collector<? super T, ?, R1> downstream1, Collector<? super T, ?, R2> downstream2, BiFunction<? super R1, ? super R2, R> merger )
在这里,我们对集合执行不同的函数,然后使用合并 BiFunction 合并结果。
考虑以下示例 -
ApiTester.java
import java.util.stream.Collectors; import java.util.stream.Stream; public class APITester { public static void main(String[] args) { double mean = Stream.of(1, 2, 3, 4, 5, 6, 7) .collect(Collectors.teeing( Collectors.summingDouble(i -> i), Collectors.counting(), (sum, n) -> sum / n)); System.out.println(mean); } }
输出
4.0