-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreamTest.java
63 lines (48 loc) · 2.24 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.github.hubertwo.playground.java16.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.IntStream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
class StreamTest {
@Test
@DisplayName("New method Stream.toList produces UnmodifiableList")
void toList() {
ImmutableList<Integer> expectedEvenNumbers = ImmutableList.of(0, 2, 4, 6, 8);
List<Integer> actualNumbers = IntStream.range(0, 10)
.filter(i -> i % 2 == 0)
.boxed()
// Try to use.collect(Collectors.toList());
.toList();
assertThat(actualNumbers).containsExactlyElementsOf(expectedEvenNumbers);
assertThrows(UnsupportedOperationException.class, () -> actualNumbers.add(10));
}
@Test
@DisplayName("Stream.mapMulti and Stream.flatMap - combine lists")
@SuppressWarnings({"Convert2MethodRef", "FunctionalExpressionCanBeFolded"}) /* For example readability */
void mapMulti_lists() {
List<String> expectedList = List.of("A", "B", "C", "D");
List<List<String>> givenListOfLists = ImmutableList.of(
List.of("A", "B"),
List.of("C", "D")
);
// multiMap
List<String> multiMapResult = givenListOfLists.stream()
.<String>mapMulti((list, downstream) -> {
// Add each element to downstream
// The point here is we decide how to add elements to downstream.
// That might be helpful when converting element to Stream directly is hard to achieve.
list.forEach(letter -> downstream.accept(letter));
})
.toList();
// flatMap
List<String> flatMapResult = givenListOfLists.stream()
// Convert each list to Stream and let flatMap combine them
.flatMap(list -> list.stream())
.toList();
assertThat(multiMapResult).containsExactlyElementsOf(expectedList);
assertThat(flatMapResult).containsExactlyElementsOf(expectedList);
}
}