-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamTest.java
36 lines (29 loc) · 1.28 KB
/
StreamTest.java
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
package com.github.hubertwo.playground.java12.streams;
import com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
class StreamTest {
@Test
@DisplayName("Collectors teeing")
void collectors_teeing() {
ImmutableList<Integer> givenList = ImmutableList.of(1, 2, 3, 4, 5, 6);
ImmutableList<List<Integer>> expectedGroupedIntegers = ImmutableList.of(
List.of(1, 2, 3),
List.of(4, 5, 6)
);
// The same might be achieved by groupBy
ImmutableList<List<Integer>> actualGroupedIntegers = givenList.stream()
.collect(Collectors.teeing(
// Putting all Ints < 4 into list
Collectors.filtering((i) -> i < 4, Collectors.toList()),
// Putting all Ints >= 4 into second list
Collectors.filtering((i) -> i >= 4, Collectors.toList()),
// Combining both lists
(l1, l2) -> ImmutableList.of(l1, l2)
));
assertThat(actualGroupedIntegers).isEqualTo(expectedGroupedIntegers);
}
}