-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathRxJavaFlatMapTest.java
More file actions
67 lines (48 loc) · 1.94 KB
/
Copy pathRxJavaFlatMapTest.java
File metadata and controls
67 lines (48 loc) · 1.94 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
58
59
60
61
62
63
64
65
66
67
package com.howtoprogram.rxjava2;
import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class RxJavaFlatMapTest {
@Test
public void flatMapObservableTest() {
List<String> sentences = new ArrayList<>();
sentences.add("Ladybug! Ladybug!");
sentences.add("Fly away home.");
sentences.add("Your house is on fire.");
sentences.add("And your children all gone.");
Observable.fromIterable(sentences)
.flatMap(s -> Observable.fromArray(s.split(" ")))
.blockingSubscribe(System.out::println);
// Observable.fromIterable(sentences)
// .flatMap(s -> Observable.fromArray(s.split(" ")), (s1, s2) -> findOccurrences(s1, s2))
// .blockingSubscribe(System.out::println);
}
@Test
public void flatMapFlowableTest() {
List<String> sentences = new ArrayList<>();
sentences.add("Fly away home.");
sentences.add("One plus one, two for life");
sentences.add("Over and over again");
Flowable.fromIterable(sentences)
.flatMap(s -> Flowable.fromArray(s.split(" ")))
.blockingSubscribe(System.out::println);
// Flowable.fromIterable(sentences)
// .flatMap(s -> Flowable.fromArray(s.split(" ")), (s1, s2) -> findOccurrences(s1, s2))
// .blockingSubscribe(System.out::println);
}
private static String findOccurrences(String str, String findStr) {
int lastIndex = 0;
int count = 0;
while (lastIndex != -1) {
lastIndex = str.indexOf(findStr, lastIndex);
if (lastIndex != -1) {
count++;
lastIndex += findStr.length();
}
}
return String.format("'%s' appears %d times in '%s' ", findStr, count, str);
}
}