logo

Teeing Collectors


Show

New methods for collectors are released in Java 12. It has to perform two different operations on collections and then merge the outcome. Below 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
)

Different functions on a collection and then merge the outcome using merger BiFunction is performed here.

Examine the Below-Given 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