-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObservableGroup.java
48 lines (43 loc) · 1.11 KB
/
ObservableGroup.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
40
41
42
43
44
45
46
47
48
package java_rxjava134;
import java.util.Arrays;
import java.util.List;
import rx.Observable;
/**
* Observable group
*
* @author ÌÆÁú
*
*/
public class ObservableGroup {
public static void main(String[] args) {
// groupBy
List<String> names = Arrays.asList(
"Bill Gates from Microsoft",
"Steve Jobs from Apple",
"The Moon Is So Bright",
"Tanglong from China",
"TomCat", "OctoCat"
);
testGroupBy(names);
testGroupBy2(names);
}
/** Test for groupBy */
@SuppressWarnings("hiding")
private static <T> void testGroupBy(List<String> list) {
Observable
.from(list)
.groupBy(e -> e.split(" ").length) // group the elements by it's length
.subscribe(e -> Commons.subscribePrint(e, e.getKey() + " word(s)"));
}
/** Another test for groupBy */
@SuppressWarnings("hiding")
private static <T> void testGroupBy2(List<String> list) {
Observable
.from(list)
.groupBy(
e -> e.replaceAll("[^mM]", "").length(),
e -> e.replaceAll("[mM]", "*")
)
.subscribe(e -> Commons.subscribePrint(e, e.getKey() + " Occurences of m"));
}
}