Skip to content

Commit 1715833

Browse files
committed
RxJava 3 FlatMap example
1 parent b87a86f commit 1715833

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

rxjava2-examples/pom.xml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.howtoprogram</groupId>
5+
<artifactId>rxjava2-examples</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<java.version>1.8</java.version>
11+
<junit.version>4.12</junit.version>
12+
<junit.jupiter.version>5.0.0</junit.jupiter.version>
13+
<junit.vintage.version>${junit.version}.0</junit.vintage.version>
14+
<junit.jupiter.version>5.0.0</junit.jupiter.version>
15+
<junit.platform.version>1.0.0</junit.platform.version>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>io.reactivex.rxjava2</groupId>
21+
<artifactId>rxjava</artifactId>
22+
<version>2.1.8</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.junit.jupiter</groupId>
26+
<artifactId>junit-jupiter-engine</artifactId>
27+
<version>${junit.jupiter.version}</version>
28+
<scope>test</scope>
29+
</dependency>
30+
<!-- To run tests on IDE such as Eclipse, Intellij -->
31+
<dependency>
32+
<groupId>junit</groupId>
33+
<artifactId>junit</artifactId>
34+
<version>${junit.version}</version>
35+
<scope>test</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.junit.platform</groupId>
39+
<artifactId>junit-platform-runner</artifactId>
40+
<version>${junit.platform.version}</version>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.junit.vintage</groupId>
45+
<artifactId>junit-vintage-engine</artifactId>
46+
<version>${junit.vintage.version}</version>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<artifactId>maven-compiler-plugin</artifactId>
55+
<version>3.1</version>
56+
<configuration>
57+
<source>${java.version}</source>
58+
<target>${java.version}</target>
59+
</configuration>
60+
</plugin>
61+
<plugin>
62+
<artifactId>maven-surefire-plugin</artifactId>
63+
<version>2.19</version>
64+
<dependencies>
65+
<dependency>
66+
<groupId>org.junit.platform</groupId>
67+
<artifactId>junit-platform-surefire-provider</artifactId>
68+
<version>${junit.platform.version}</version>
69+
</dependency>
70+
</dependencies>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
</project>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.howtoprogram.rxjava2;
2+
3+
import io.reactivex.Flowable;
4+
import io.reactivex.Observable;
5+
import io.reactivex.functions.Consumer;
6+
import org.junit.Test;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
public class RxJavaFlatMapTest {
12+
13+
@Test
14+
public void flatMapObservableTest() {
15+
16+
List<String> sentences = new ArrayList<>();
17+
sentences.add("Ladybug! Ladybug!");
18+
sentences.add("Fly away home.");
19+
sentences.add("Your house is on fire.");
20+
sentences.add("And your children all gone.");
21+
22+
Observable.fromIterable(sentences)
23+
.flatMap(s -> Observable.fromArray(s.split(" ")))
24+
.blockingSubscribe(System.out::println);
25+
26+
// Observable.fromIterable(sentences)
27+
// .flatMap(s -> Observable.fromArray(s.split(" ")), (s1, s2) -> findOccurrences(s1, s2))
28+
// .blockingSubscribe(System.out::println);
29+
30+
}
31+
@Test
32+
public void flatMapFlowableTest() {
33+
34+
List<String> sentences = new ArrayList<>();
35+
sentences.add("Fly away home.");
36+
sentences.add("One plus one, two for life");
37+
sentences.add("Over and over again");
38+
39+
Flowable.fromIterable(sentences)
40+
.flatMap(s -> Flowable.fromArray(s.split(" ")))
41+
.blockingSubscribe(System.out::println);
42+
43+
44+
// Flowable.fromIterable(sentences)
45+
// .flatMap(s -> Flowable.fromArray(s.split(" ")), (s1, s2) -> findOccurrences(s1, s2))
46+
// .blockingSubscribe(System.out::println);
47+
48+
}
49+
50+
private static String findOccurrences(String str, String findStr) {
51+
52+
int lastIndex = 0;
53+
int count = 0;
54+
55+
while (lastIndex != -1) {
56+
57+
lastIndex = str.indexOf(findStr, lastIndex);
58+
59+
if (lastIndex != -1) {
60+
count++;
61+
lastIndex += findStr.length();
62+
}
63+
}
64+
return String.format("'%s' appears %d times in '%s' ", findStr, count, str);
65+
}
66+
67+
}

0 commit comments

Comments
 (0)