-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamExample.java
More file actions
26 lines (23 loc) · 1.27 KB
/
Copy pathStreamExample.java
File metadata and controls
26 lines (23 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package Java12;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class StreamExample {
public static void main(String[] args) {
//Collectors.teeing
//Collectors.teeing() is a new collector added in Java 12. It is used to combine the result of two collectors into a single result.
//The first argument is the first collector, the second argument is the second collector, and the third argument is a function that combines the results of the two collectors.
//The function should not be null.
//Collectors.teeing() is used to calculate the average of the list of integers.
//The first collector calculates the sum of the list of integers.
//The second collector calculates the count of the list of integers.
//The third argument is a function that calculates the average of the sum and count.
//The function takes two arguments, the sum and the count, and returns the average.
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
Double average = list.stream().collect(
Collectors.teeing(Collectors.summingDouble(i -> i),
Collectors.counting(), (sum, count) -> sum / count));
System.out.println("Average = " + average);
}
}