This tutorial explains about teeing method in java.util.stream.Collectors in java12, with syntax, how to use this method, and examples.

## Collectors teeing method

teeing method is used to collect the stream of data using two downstream collectors and merge the data with BiFunction and return the result.

It is a static method that can be called directly using Collectors Class. Here is a syntax

```
public static <T, R1, R2, R>
Collector<T, ?, R> teeing(Collector<? super T, ?, R1> downstream1,
Collector<? super T, ?, R2> downstream2,
BiFunction<? super R1, ? super R2, R> merger)
```

It contains two collections downstream1: first collector downstream2: second collector

One function to merge the result

The return value is an aggregated result of downstream collectors.

Let’s see an example to find the Average of numbers.

To find the Average of numbers, First We have to sum the numbers, second find out the number of elements, and Finally divide the sum by count to get an average.

Here is an example code

```
package java12;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class TeeingTest {
public static void main(String[] args) {
var stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
// one collector is to sum of stream of numbers
// second collector is to find the count for stream of numbers
// Function that received the result of this two collectors and calculate average and return the result
var result = stream.collect(Collectors.teeing(
Collectors.summingInt(value -> value), Collectors.counting(), (sum, count) -> sum / count)
);
System.out.println(result);
}
}
```

output:

```
5
```

Find the maximum and minimum of a given list of numbers For Example

```
package java12;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class TeeingTestMaxMin {
public static void main(String[] args) {
var stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
// one collector is to Find maximum from a stream of numbers
// second collector is to find minimum from a stream of numbers
// Function that received the result of this two collectors and returns result
var result = stream.collect(Collectors.teeing(
Collectors.maxBy(Comparator.comparing(i -> i)),
Collectors.minBy(Comparator.comparing(i -> i)),
(max, min) -> {
return List.of(max.get(), min.get());
}));
System.out.println(result);
}
}
```

Output:

```
[9, 1]
```