Tue. Apr 23rd, 2024

Java 12 introduced a new collector called the Teeing collector. This collector allows you to process elements with two different collectors simultaneously and combine the results into a single output. In this article, we’ll take a closer look at the Teeing collector and how you can use it in your Java code.

What is the Teeing collector?

The Teeing collector is a specialized form of the Collectors class, which provides a variety of collectors for use with streams. The Teeing collector takes three arguments: two collectors and a merging function. The first collector processes the elements of the stream and produces a result of type T1, while the second collector processes the same elements and produces a result of type T2. The merging function then takes the two results and combines them into a single result of type R.

Here’s the signature of the Teeing collector:

public static <T, T1, T2, R> Collector<T, ?, R> teeing(     Collector<? super T, ?, T1> downstream1,     Collector<? super T, ?, T2> downstream2,     BiFunction<? super T1, ? super T2, ? extends R> merger)

As you can see, the Teeing collector takes two downstream collectors and a merging function. The first and second collectors represent the two calculations that you want to perform on the stream elements, while the merging function combines the results of the two collectors into a single output.

Using the Teeing collector

Let’s look at an example to see how the Teeing collector works in practice. Suppose we have a list of integers and we want to calculate the sum and the average of the elements, and then use these values to calculate the sum divided by the average.

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream().collect(Collectors.summingInt(Integer::intValue));
double average = numbers.stream().collect(Collectors.averagingInt(Integer::intValue));
double result = numbers.stream().collect(
	Collectors.teeing( Collectors.summingInt(Integer::intValue),
		Collectors.averagingInt(Integer::intValue),
		(sumVal, avgVal) -> sumVal / avgVal
	)
);

In this example, we first use the summingInt collector to calculate the sum of the elements, and the averagingInt collector to calculate the average of the elements. We then use the Teeing collector to combine these values into a single output by dividing the sum by the average.

Benefits of the Teeing collector

The Teeing collector provides a convenient way to perform two separate calculations on the same data and combine the results into a single output. This can be useful in a variety of scenarios where multiple statistics or calculations need to be performed on the same data.

The Teeing collector is also very easy to use. You simply pass in the two collectors and the merging function, and the collector takes care of the rest. This makes it a great tool for stream processing, where you often want to perform multiple calculations on the same data.

Conclusion

The Teeing collector is a powerful new feature introduced in Java 12 that allows you to process elements with two different collectors simultaneously and combine the results into a single output. In this article, we’ve looked at how the Teeing collector works and how you can use it in your Java code. By using the Teeing collector, you can perform multiple calculations on the same data and combine the results into a single output, making your code more efficient and easier to read.

By Jeffery Miller

I am known for being able to quickly decipher difficult problems to assist development teams in producing a solution. I have been called upon to be the Team Lead for multiple large-scale projects. I have a keen interest in learning new technologies, always ready for a new challenge.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.