Java 12 – Teeing Collectors

  • Post author:
  • Post category:Java 12
  • Post comments:1 Comment
Java 12 - Teeing Collectors

This topic is about 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

In this topic we learned about Java 12 – Teeing Collectors. To know more, Click Here.

This Post Has One Comment

  1. finance blog

    I am grateful for your post. I would really like to comment that the expense of car insurance differs from one insurance plan to another, given that there are so many different facets which contribute to the overall cost. For example, the brand name of the car will have a huge bearing on the price. A reliable old family auto will have a more economical premium than just a flashy fancy car.

Leave a Reply