-
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathObservableCollect.java
More file actions
35 lines (25 loc) · 862 Bytes
/
ObservableCollect.java
File metadata and controls
35 lines (25 loc) · 862 Bytes
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
package rx.observables.transforming;
import org.junit.Test;
import rx.Observable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author Pablo Perez
*/
/**
* Collect operator include constantClass function where we define the init value, and constantClass BiConsumer function where we
* receive the accumulator to mutate and the item emitted, which we should add into the accumulator
*/
public class ObservableCollect {
@Test
public void collectObservableList() {
Observable.from(Arrays.asList(1, 2))
.flatMap(item -> getFirstList())
.collect(ArrayList<Integer>::new, ArrayList::addAll)
.subscribe(System.out::println);
}
private Observable<List<Integer>> getFirstList() {
return Observable.just(Arrays.asList(1, 2, 3, 4));
}
}