forked from wesleyegberto/java-new-features
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectorsTeeingTest.java
More file actions
37 lines (31 loc) · 807 Bytes
/
CollectorsTeeingTest.java
File metadata and controls
37 lines (31 loc) · 807 Bytes
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
27
28
29
30
31
32
33
34
35
36
37
import java.util.stream.*;
import java.util.Optional;
public class CollectorsTeeingTest {
public static void main(String[] args) {
Range range = Stream
.of(1, 8, 2, 5)
.collect(Collectors.teeing(
// the collectors produce Optional<Integer>
Collectors.minBy(Integer::compareTo),
Collectors.maxBy(Integer::compareTo),
// merger
Range::ofOptional)
);
System.out.println("Range: " + range.getRange());
}
}
class Range {
private String range;
Range(String range) {
this.range = range;
}
public static Range ofOptional(Optional<Integer> min, Optional<Integer> max) {
if (min.isEmpty() || max.isEmpty()) {
return new Range("EMPTY");
}
return new Range(String.format("from %d to %d", min.get(), max.get()));
}
public String getRange() {
return range;
}
}