
- 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
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java 12 - Teeing Collectors
Java 12 introduces a new method to Collectors to perform two different operations on collection and then merge the result. Folloiwng is the syntax of teeing method −
Collector<T, ?, R> teeing( Collector<? super T, ?, R1> downstream1, Collector<? super T, ?, R2> downstream2, BiFunction<? super R1, ? super R2, R> merger )
Here we are performing different functions on a collection and then merge the result using merger BiFunction.
Consider the following example −
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); } }
Output
4.0
Advertisements