Skip to content

Commit a34d4f0

Browse files
authored
Merge pull request eugenp#6569 from alimate/BAEL-2786
Teeing Collector
2 parents 199334a + 18eb32a commit a34d4f0

3 files changed

Lines changed: 126 additions & 0 deletions

File tree

‎core-java-12/pom.xml‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>core-java-12</artifactId>
8+
<version>0.1.0-SNAPSHOT</version>
9+
<name>core-java-12</name>
10+
<packaging>jar</packaging>
11+
<url>http://maven.apache.org</url>
12+
13+
<parent>
14+
<groupId>com.baeldung</groupId>
15+
<artifactId>parent-modules</artifactId>
16+
<version>1.0.0-SNAPSHOT</version>
17+
</parent>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.assertj</groupId>
22+
<artifactId>assertj-core</artifactId>
23+
<version>${assertj.version}</version>
24+
<scope>test</scope>
25+
</dependency>
26+
</dependencies>
27+
28+
<build>
29+
<plugins>
30+
<plugin>
31+
<groupId>org.apache.maven.plugins</groupId>
32+
<artifactId>maven-compiler-plugin</artifactId>
33+
<version>${maven-compiler-plugin.version}</version>
34+
<configuration>
35+
<source>${maven.compiler.source.version}</source>
36+
<target>${maven.compiler.target.version}</target>
37+
</configuration>
38+
</plugin>
39+
</plugins>
40+
</build>
41+
42+
<properties>
43+
<maven.compiler.source.version>12</maven.compiler.source.version>
44+
<maven.compiler.target.version>12</maven.compiler.target.version>
45+
<assertj.version>3.6.1</assertj.version>
46+
</properties>
47+
48+
</project>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.baeldung.collectors;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Objects;
6+
7+
import org.junit.Test;
8+
9+
import static java.util.stream.Collectors.maxBy;
10+
import static java.util.stream.Collectors.minBy;
11+
import static java.util.stream.Collectors.teeing;
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
/**
15+
* Unit tests for collectors additions in Java 12.
16+
*/
17+
public class CollectorsUnitTest {
18+
19+
@Test
20+
public void whenTeeing_ItShouldCombineTheResultsAsExpected() {
21+
List<Integer> numbers = Arrays.asList(42, 4, 2, 24);
22+
Range range = numbers.stream()
23+
.collect(teeing(
24+
minBy(Integer::compareTo),
25+
maxBy(Integer::compareTo),
26+
(min, max) -> new Range(min.orElse(null), max.orElse(null))
27+
));
28+
29+
assertThat(range).isEqualTo(new Range(2, 42));
30+
}
31+
32+
/**
33+
* Represents a closed range of numbers between {@link #min} and
34+
* {@link #max}, both inclusive.
35+
*/
36+
private static class Range {
37+
38+
private final Integer min;
39+
40+
private final Integer max;
41+
42+
Range(Integer min, Integer max) {
43+
this.min = min;
44+
this.max = max;
45+
}
46+
47+
Integer getMin() {
48+
return min;
49+
}
50+
51+
Integer getMax() {
52+
return max;
53+
}
54+
55+
@Override
56+
public boolean equals(Object o) {
57+
if (this == o) return true;
58+
if (o == null || getClass() != o.getClass()) return false;
59+
Range range = (Range) o;
60+
return Objects.equals(getMin(), range.getMin()) &&
61+
Objects.equals(getMax(), range.getMax());
62+
}
63+
64+
@Override
65+
public int hashCode() {
66+
return Objects.hash(getMin(), getMax());
67+
}
68+
69+
@Override
70+
public String toString() {
71+
return "Range{" +
72+
"min=" + min +
73+
", max=" + max +
74+
'}';
75+
}
76+
}
77+
}

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@
377377
<module>core-groovy</module>
378378
<!-- <module>core-java-10</module> --> <!-- We haven't upgraded to java 10. Fixing in BAEL-10841 -->
379379
<!-- <module>core-java-11</module> --> <!-- We haven't upgraded to java 11. Fixing in BAEL-10841 -->
380+
<!-- <module>core-java-12</module> --> <!-- We haven't upgraded to java 12. Fixing in BAEL-10841 -->
380381
<module>core-java-8</module>
381382
<!--<module>core-java-9</module> --> <!-- We haven't upgraded to java 9. Fixing in BAEL-10841 -->
382383
<!--<module>core-java-os</module> --> <!-- We haven't upgraded to java 9.-->

0 commit comments

Comments
 (0)