forked from PacktPublishing/Learning-RxJava-Second-Edition
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh9_01b.java
More file actions
23 lines (19 loc) · 868 Bytes
/
Copy pathCh9_01b.java
File metadata and controls
23 lines (19 loc) · 868 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import com.google.common.collect.ImmutableList;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableTransformer;
public class Ch9_01b {
public static void main(String[] args) {
Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon")
.compose(toImmutableList())
.subscribe(System.out::println);
Observable.range(1, 10)
.compose(toImmutableList())
.subscribe(System.out::println);
}
private static <T> ObservableTransformer<T, ImmutableList<T>> toImmutableList() {
return upstream ->
upstream.collect(ImmutableList::<T> builder, ImmutableList.Builder::add)
.map(ImmutableList.Builder::build)
.toObservable(); // must turn Single into Observable
}
}