-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample7.java
More file actions
49 lines (40 loc) · 1.93 KB
/
example7.java
File metadata and controls
49 lines (40 loc) · 1.93 KB
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
import rx.Observable;
import java.util.List;
/**
*
* Create two Observables
* Merge them
* Reduce the two results into one
* Map the result to the format you want
* Print it to screen using method subscribe()
*
* Created by matie on 28/04/15.
*/
public class example7 {
private static Integer[] firstList = {1,3,5};
private static Integer[] secondList = {2,4,6};
private static Observable<Integer> firstObservable = Observable.from(firstList);
private static Observable<Integer> secondObservable = Observable.from(secondList);
public static void main(String[] args){
//reduce() will sum first item with the second, combined with the third, and so on
Observable.merge(firstObservable, secondObservable)
.reduce((first, second) -> first + second)
.map(num -> "Sum of items: " + num)
.subscribe(message -> System.out.println(message));
//reduce() with an initialValue will sum [initial + (first) + (second) + (third)...]
int initialValue = 10;
Observable.merge(firstObservable, secondObservable)
.reduce(initialValue, (first, second) -> first + second)
.map(num -> "Sum with initial value of 10: " + num)
.subscribe(message -> System.out.println(message));
//reduce() with an initialValue will sum [initial + (initial+first) + (initial+second) + (initial+third)...]
Observable.merge(firstObservable, secondObservable)
.reduce(initialValue, (first, second) -> initialValue + first + second)
.map(num -> "Sum with initial value of 10 in function: " + num)
.subscribe(message -> System.out.println(message));
//observe how the two observables become one
Observable.merge(firstObservable, secondObservable)
.map(num -> "Single item: " + num)
.subscribe(message -> System.out.println(message));
}
}