-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGatherers_7.snippet
33 lines (33 loc) · 1.01 KB
/
Gatherers_7.snippet
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
// Inserting an element into a stream
interface GathererSupplier {
String get();
default Gatherer<String, ?, String> at(int insertionIndex) {
return Gatherer.ofSequential(
() -> new Object() {
int index;
final String element = get();
},
(state, s, stream) -> {
if (state.index != insertionIndex) {
state.index++;
return stream.push(s);
} else {
stream.push(state.element);
stream.push(s);
state.index++;
return true;
}
}
);
}
}
GathererSupplier insert(String element) {
return () -> element;
}
var ints = List.of("one", "two", "three", "four", "five");
var index = 3;
var element = "something";
var result = ints.stream()
.gather(insert(element).at(index))
.toList();
System.out.println("result = " + result);