-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObservableOtherMethods.java
39 lines (35 loc) · 1.68 KB
/
ObservableOtherMethods.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
package java_rxjava134;
import static java_rxjava134.Commons.DEFAULT_VALUE;
import static java_rxjava134.Commons.subscribePrint;
import java.util.Arrays;
import rx.Observable;
/**
* Other methods
*
* @author ÌÆÁú
*
*/
public class ObservableOtherMethods {
public static void main(String[] args) {
Observable<Integer> numbers = Observable.just(13, 23, 8, 55, 23);
// TakeLast series
subscribePrint(numbers.last(), "Last"); // get the last item
subscribePrint(numbers.first(), "First"); // get the first item
subscribePrint(numbers.take(3), "Take 3"); // get the first three items
subscribePrint(numbers.skipLast(3), "SkipLast 3"); //skip the last three items
subscribePrint(numbers.takeLast(3), "TakeLast 3"); // get the last three items
subscribePrint(numbers.takeLastBuffer(4), "TakeLastBuffer 3");
subscribePrint(numbers.lastOrDefault(DEFAULT_VALUE), "LastOrDefault"); // get the last item or the defaut one
subscribePrint(Observable.empty().lastOrDefault(DEFAULT_VALUE), "LastOrDefault");
subscribePrint(numbers.elementAt(3), "ElementAt 3"); // get item at specified position
subscribePrint(numbers.elementAtOrDefault(99, DEFAULT_VALUE), "ElementAtOrDefault 3");
subscribePrint(numbers.distinct(), "Distinct"); // get all items excluding repeated ones
subscribePrint(numbers.distinctUntilChanged(), "DistinctUntilChanged"); // get all items excluding one that is same to it's predecessor
// get items that meet the specified type
// 'ofType' is shortcut of 'filter(v -> Class.isInstance(v)).'
subscribePrint(
Observable
.from(Arrays.asList('c', 1, "RxJava" , 5, true, 8))
.ofType(Integer.class), "OfType Integer");
}
}