-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathUsingGroupBy.java
More file actions
57 lines (43 loc) · 1.26 KB
/
UsingGroupBy.java
File metadata and controls
57 lines (43 loc) · 1.26 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
50
51
52
53
54
55
56
57
package com.packtpub.reactive.chapter04;
import static com.packtpub.reactive.common.Helpers.subscribePrint;
import java.util.Arrays;
import java.util.List;
import rx.Observable;
import com.packtpub.reactive.common.Program;
/**
* Demonstrates how the groupBy operator can be used.
*
* @author meddle
*/
public class UsingGroupBy implements Program {
@Override
public String name() {
return "Demonstration of using the Observable#groupBy operator";
}
@Override
public int chapter() {
return 4;
}
@Override
public void run() {
List<String> albums = Arrays.asList(
"The Piper at the Gates of Dawn", "A Saucerful of Secrets", "More",
"Ummagumma", "Atom Heart Mother", "Meddle", "Obscured by Clouds",
"The Dark Side of the Moon", "Wish You Were Here", "Animals",
"The Wall");
Observable.from(albums).groupBy(album -> album.split(" ").length)
.subscribe(obs -> {
subscribePrint(obs, obs.getKey() + " word(s)");
});
Observable
.from(albums)
.groupBy(album -> album.replaceAll("[^mM]", "").length(),
album -> album.replaceAll("[mM]", "*"))
.subscribe(
obs -> subscribePrint(obs, obs.getKey()
+ " occurences of 'm'"));
}
public static void main(String[] args) {
new UsingGroupBy().run();
}
}