├── .gitignore ├── Math ├── operators │ ├── sum.png │ ├── count.png │ ├── reduce.png │ └── average.png └── src │ └── Main.java ├── .idea ├── copyright │ └── profiles_settings.xml ├── vcs.xml ├── encodings.xml ├── runConfigurations │ ├── Math_Main.xml │ ├── Combining_Main.xml │ ├── Filtering_Main.xml │ ├── Conditional_Main.xml │ ├── ErrorHandling_Main.xml │ ├── Transforming_Main.xml │ └── App.xml ├── modules.xml ├── misc.xml └── compiler.xml ├── Combining ├── operators │ ├── amb.png │ ├── zip.png │ ├── concat.png │ ├── merge.png │ ├── startWith.png │ ├── combineLatest.png │ └── sequenceEqual.png └── src │ └── Main.java ├── Conditional ├── operators │ ├── all.png │ ├── contains.png │ ├── skipUntil.png │ ├── skipWhile.png │ ├── takeUntil.png │ └── takeWhile.png └── src │ └── Main.java ├── Filtering ├── operators │ ├── filter.png │ ├── first.png │ ├── last.png │ ├── skip.png │ ├── take.png │ ├── debounce.png │ ├── distinct.png │ ├── skipLast.png │ ├── takeLast.png │ ├── elementAt.png │ ├── ignoreElements.png │ └── distinctUntilChanged.png └── src │ └── Main.java ├── Transforming ├── operators │ ├── map.png │ ├── scan.png │ ├── buffer.png │ ├── flatMap.png │ └── groupBy.png └── src │ └── Main.java ├── ErrorHandling ├── operators │ ├── onErrorReturn.png │ └── onErrorResumeNext.png └── src │ └── Main.java ├── src ├── main │ └── java │ │ └── com │ │ └── tunjos │ │ └── rxjava2 │ │ ├── App.java │ │ └── NumberUtils.java └── test │ └── java │ └── com │ └── tunjos │ └── rxjava2 │ └── AppTest.java ├── pom.xml └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | /out 3 | /.idea/workspace.xml 4 | /.idea/libraries 5 | -------------------------------------------------------------------------------- /Math/operators/sum.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Math/operators/sum.png -------------------------------------------------------------------------------- /Math/operators/count.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Math/operators/count.png -------------------------------------------------------------------------------- /Math/operators/reduce.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Math/operators/reduce.png -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /Combining/operators/amb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Combining/operators/amb.png -------------------------------------------------------------------------------- /Combining/operators/zip.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Combining/operators/zip.png -------------------------------------------------------------------------------- /Math/operators/average.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Math/operators/average.png -------------------------------------------------------------------------------- /Combining/operators/concat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Combining/operators/concat.png -------------------------------------------------------------------------------- /Combining/operators/merge.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Combining/operators/merge.png -------------------------------------------------------------------------------- /Conditional/operators/all.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Conditional/operators/all.png -------------------------------------------------------------------------------- /Filtering/operators/filter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Filtering/operators/filter.png -------------------------------------------------------------------------------- /Filtering/operators/first.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Filtering/operators/first.png -------------------------------------------------------------------------------- /Filtering/operators/last.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Filtering/operators/last.png -------------------------------------------------------------------------------- /Filtering/operators/skip.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Filtering/operators/skip.png -------------------------------------------------------------------------------- /Filtering/operators/take.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Filtering/operators/take.png -------------------------------------------------------------------------------- /Transforming/operators/map.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Transforming/operators/map.png -------------------------------------------------------------------------------- /Filtering/operators/debounce.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Filtering/operators/debounce.png -------------------------------------------------------------------------------- /Filtering/operators/distinct.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Filtering/operators/distinct.png -------------------------------------------------------------------------------- /Filtering/operators/skipLast.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Filtering/operators/skipLast.png -------------------------------------------------------------------------------- /Filtering/operators/takeLast.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Filtering/operators/takeLast.png -------------------------------------------------------------------------------- /Transforming/operators/scan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Transforming/operators/scan.png -------------------------------------------------------------------------------- /Combining/operators/startWith.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Combining/operators/startWith.png -------------------------------------------------------------------------------- /Conditional/operators/contains.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Conditional/operators/contains.png -------------------------------------------------------------------------------- /Conditional/operators/skipUntil.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Conditional/operators/skipUntil.png -------------------------------------------------------------------------------- /Conditional/operators/skipWhile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Conditional/operators/skipWhile.png -------------------------------------------------------------------------------- /Conditional/operators/takeUntil.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Conditional/operators/takeUntil.png -------------------------------------------------------------------------------- /Conditional/operators/takeWhile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Conditional/operators/takeWhile.png -------------------------------------------------------------------------------- /Filtering/operators/elementAt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Filtering/operators/elementAt.png -------------------------------------------------------------------------------- /Transforming/operators/buffer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Transforming/operators/buffer.png -------------------------------------------------------------------------------- /Transforming/operators/flatMap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Transforming/operators/flatMap.png -------------------------------------------------------------------------------- /Transforming/operators/groupBy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Transforming/operators/groupBy.png -------------------------------------------------------------------------------- /Combining/operators/combineLatest.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Combining/operators/combineLatest.png -------------------------------------------------------------------------------- /Combining/operators/sequenceEqual.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Combining/operators/sequenceEqual.png -------------------------------------------------------------------------------- /Filtering/operators/ignoreElements.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Filtering/operators/ignoreElements.png -------------------------------------------------------------------------------- /ErrorHandling/operators/onErrorReturn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/ErrorHandling/operators/onErrorReturn.png -------------------------------------------------------------------------------- /ErrorHandling/operators/onErrorResumeNext.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/ErrorHandling/operators/onErrorResumeNext.png -------------------------------------------------------------------------------- /Filtering/operators/distinctUntilChanged.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tunjos/RxJava2-RxMarbles-Samples/HEAD/Filtering/operators/distinctUntilChanged.png -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/main/java/com/tunjos/rxjava2/App.java: -------------------------------------------------------------------------------- 1 | package com.tunjos.rxjava2; 2 | 3 | 4 | public class App { 5 | public static void main( String[] args ) { 6 | System.out.println("Hello RxJava2"); 7 | } 8 | } -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/main/java/com/tunjos/rxjava2/NumberUtils.java: -------------------------------------------------------------------------------- 1 | package com.tunjos.rxjava2; 2 | 3 | 4 | public class NumberUtils { 5 | 6 | public static boolean isEven(int number) { 7 | return number % 2 == 0; 8 | } 9 | 10 | public static boolean isPrime(int number) { 11 | if (number % 2 == 0) return false; 12 | 13 | for (int i = 3; i*i <= number; i+=2) { 14 | if (number % i == 0) 15 | return false; 16 | } 17 | return true; 18 | } 19 | } -------------------------------------------------------------------------------- /src/test/java/com/tunjos/rxjava2/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.tunjos.rxjava2; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest extends TestCase { 11 | /** 12 | * Create the test case 13 | * 14 | * @param testName name of the test case 15 | */ 16 | public AppTest(String testName) { 17 | super(testName); 18 | } 19 | 20 | /** 21 | * @return the suite of tests being tested 22 | */ 23 | public static Test suite() { 24 | return new TestSuite(AppTest.class); 25 | } 26 | 27 | /** 28 | * Rigourous Test :-) 29 | */ 30 | public void testApp() { 31 | assertTrue(true); 32 | } 33 | } -------------------------------------------------------------------------------- /.idea/runConfigurations/Math_Main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | -------------------------------------------------------------------------------- /.idea/runConfigurations/Combining_Main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | -------------------------------------------------------------------------------- /.idea/runConfigurations/Filtering_Main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | -------------------------------------------------------------------------------- /.idea/runConfigurations/Conditional_Main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | -------------------------------------------------------------------------------- /.idea/runConfigurations/ErrorHandling_Main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | -------------------------------------------------------------------------------- /.idea/runConfigurations/Transforming_Main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /.idea/runConfigurations/App.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 8 | 9 | 22 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.tunjos.rxjava2 6 | rxJava2-rxmarbles-samples 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | rxJava2-rxmarbles-samples 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 16 | 17 | 18 | 19 | junit 20 | junit 21 | 3.8.1 22 | test 23 | 24 | 25 | 26 | io.reactivex.rxjava2 27 | rxjava 28 | 2.1.1 29 | 30 | 31 | 32 | com.github.akarnokd 33 | rxjava2-extensions 34 | 0.17.2 35 | 36 | 37 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /ErrorHandling/src/Main.java: -------------------------------------------------------------------------------- 1 | import io.reactivex.Observable; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | 7 | public class Main { 8 | public static void main( String[] args ) { 9 | onErrorReturn(12); 10 | onErrorResumeNext(); 11 | } 12 | 13 | /** 14 | * Instruct an Observable to emit a particular item when it encounters an error, and 15 | * then terminate normally. 16 | **/ 17 | private static void onErrorReturn(Integer item) { 18 | System.out.println("onErrorReturn"); 19 | 20 | List integerList = Arrays.asList(1, 2, 3); 21 | System.out.println(integerList); 22 | Observable integerObservable = Observable.fromIterable(integerList); 23 | 24 | System.out.println("onErrorReturn(" + item + ") =====================>"); 25 | 26 | integerObservable.doAfterNext(integer -> { 27 | if (integer == 3) { 28 | throw new RuntimeException(); 29 | } 30 | }).onErrorReturn(throwable -> item) 31 | .subscribe(System.out::println); 32 | System.out.println(); 33 | } 34 | 35 | /** 36 | * Instruct an Observable to begin emitting a second Observable if it encounters an 37 | * error. 38 | **/ 39 | private static void onErrorResumeNext() { 40 | System.out.println("onErrorResumeNext"); 41 | 42 | List stringList = Arrays.asList("1", "2", "4", "5"); 43 | List stringList1 = Arrays.asList("A", "B", "C"); 44 | System.out.println(stringList); 45 | System.out.println(stringList1); 46 | Observable stringObservable = Observable.fromIterable(stringList); 47 | Observable stringObservable1 = Observable.fromIterable(stringList1); 48 | 49 | System.out.println("onErrorResumeNext =====================>"); 50 | 51 | stringObservable.doAfterNext(s -> { 52 | if (s.equals("2")) { 53 | throw new RuntimeException(); 54 | } 55 | }).onErrorResumeNext(stringObservable1) 56 | .subscribe(System.out::println); 57 | System.out.println(); 58 | } 59 | } -------------------------------------------------------------------------------- /Math/src/Main.java: -------------------------------------------------------------------------------- 1 | import hu.akarnokd.rxjava2.math.MathObservable; 2 | import io.reactivex.Observable; 3 | 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | 8 | public class Main { 9 | public static void main( String[] args ) { 10 | average(); 11 | sum(); 12 | reduce(); 13 | count(); 14 | } 15 | 16 | /** 17 | * Emit the average of numbers emitted by an Observable and terminate. 18 | * 19 | **/ 20 | private static void average() { 21 | System.out.println("average"); 22 | 23 | List integerList = Arrays.asList(1, 3); 24 | System.out.println(integerList); 25 | Observable integerObservable = Observable.fromIterable(integerList); 26 | 27 | System.out.println("average =====================>"); 28 | 29 | MathObservable.averageFloat(integerObservable) 30 | .subscribe(System.out::println); 31 | System.out.println(); 32 | } 33 | 34 | /** 35 | * Emit the sum of numbers emitted by an Observable and terminate. 36 | * 37 | **/ 38 | private static void sum() { 39 | System.out.println("sum"); 40 | 41 | List integerList = Arrays.asList(1, 2, 3); 42 | System.out.println(integerList); 43 | Observable integerObservable = Observable.fromIterable(integerList); 44 | 45 | System.out.println("sum =====================>"); 46 | 47 | MathObservable.sumInt(integerObservable) 48 | .subscribe(System.out::println); 49 | System.out.println(); 50 | } 51 | 52 | /** 53 | * Apply a function to each item emitted by an Observable, sequentially, emit the 54 | * final value and terminate. 55 | **/ 56 | private static void reduce() { 57 | System.out.println("reduce"); 58 | 59 | List integerList = Arrays.asList(1, 2, 3); 60 | System.out.println(integerList); 61 | Observable integerObservable = Observable.fromIterable(integerList); 62 | 63 | System.out.println("reduce((x, y) => x * y) =====================>"); 64 | 65 | integerObservable.reduce((x, y) -> x * y) 66 | .subscribe(System.out::println); 67 | System.out.println(); 68 | } 69 | 70 | /** 71 | * Emit the count of items emitted by an Observable and terminate. 72 | * 73 | **/ 74 | private static void count() { 75 | System.out.println("count"); 76 | 77 | List integerList = Arrays.asList(4, 8); 78 | System.out.println(integerList); 79 | Observable integerObservable = Observable.fromIterable(integerList); 80 | 81 | System.out.println("count =====================>"); 82 | 83 | integerObservable.count() 84 | .subscribe(System.out::println); 85 | System.out.println(); 86 | } 87 | } -------------------------------------------------------------------------------- /Transforming/src/Main.java: -------------------------------------------------------------------------------- 1 | import io.reactivex.Observable; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | 7 | public class Main { 8 | public static void main( String[] args ) { 9 | map(); 10 | flatMap(); 11 | buffer(2); 12 | groupBy(); 13 | scan(); 14 | } 15 | 16 | /** 17 | * Transform the items emitted by an Observable by applying a function to each item. 18 | * 19 | **/ 20 | private static void map() { 21 | System.out.println("map"); 22 | 23 | List integerList = Arrays.asList(1, 2, 3); 24 | System.out.println(integerList); 25 | Observable integerObservable = Observable.fromIterable(integerList); 26 | 27 | System.out.println("map(x => 10 * x) =====================>"); 28 | 29 | integerObservable.map(x -> 10 * x) 30 | .subscribe(System.out::println); 31 | System.out.println(); 32 | } 33 | 34 | /** 35 | * Transform the items emitted by an Observable into Observables, then flatten the 36 | * emissions from those into a single Observable. 37 | **/ 38 | private static void flatMap() { 39 | System.out.println("flatMap"); 40 | 41 | List oneList = Arrays.asList(11, 12); 42 | List twoList = Arrays.asList(21, 22); 43 | List fourList = Arrays.asList(41, 42); 44 | List> integerListList = Arrays.asList(oneList, twoList, fourList); 45 | integerListList.forEach(System.out::println); 46 | Observable> integerListObservable = Observable.fromIterable(integerListList); 47 | 48 | System.out.println("flatMap =====================>"); 49 | 50 | integerListObservable.flatMap(Observable::fromIterable).subscribe(System.out::println); 51 | System.out.println(); 52 | } 53 | 54 | /** 55 | * Periodically gather items from an Observable into bundles and emit these bundles 56 | * rather than emitting the items one at a time. 57 | **/ 58 | private static void buffer(int count) { 59 | System.out.println("buffer"); 60 | 61 | List stringList = Arrays.asList("P", "C", "A"); 62 | System.out.println(stringList); 63 | Observable stringObservable = Observable.fromIterable(stringList); 64 | 65 | System.out.println("buffer(" + count + ") =====================>"); 66 | 67 | stringObservable.buffer(count) 68 | .subscribe(System.out::println); 69 | System.out.println(); 70 | } 71 | 72 | /** 73 | * Divide an Observable into a set of Observables that each emit a different group 74 | * of items from the original Observable, organized by key. 75 | **/ 76 | private static void groupBy() { 77 | System.out.println("groupBy"); 78 | 79 | List integerList = Arrays.asList(2, 4, 2, 6, 4, 6); 80 | System.out.println(integerList); 81 | Observable integerObservable = Observable.fromIterable(integerList); 82 | 83 | System.out.println("groupBy(x => x/2) =====================>"); 84 | 85 | integerObservable.groupBy(integer -> integer/2) 86 | .subscribe(integerIntegerGroupedObservable -> integerIntegerGroupedObservable.toList() 87 | .subscribe(System.out::println)); 88 | System.out.println(); 89 | } 90 | 91 | /** 92 | * Apply a function to each item emitted by an Observable, sequentially, and emit 93 | * each successive value. 94 | **/ 95 | private static void scan() { 96 | System.out.println("scan"); 97 | 98 | List integerList = Arrays.asList(1, 2, 3); 99 | System.out.println(integerList); 100 | Observable integerObservable = Observable.fromIterable(integerList); 101 | 102 | System.out.println("scan((x, y) => x + y) =====================>"); 103 | 104 | integerObservable.scan((x, y) -> x + y) 105 | .subscribe(System.out::println); 106 | System.out.println(); 107 | } 108 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | RxJava 2 RxMarbles Samples 2 | ============== 3 | 4 | This repository contains RxJava 2 implementations of the sample operators found in the [RxMarbles Android Application](https://play.google.com/store/apps/details?id=com.moonfleet.rxmarbles). Please download the app for a more interactive tutorial. 5 | 6 | ### Running 7 | 8 | Simply import the project using intelliJ IDEA and run the corresponding run configurations. 9 | 10 | ### Awesome Links 11 | [RxMarbles Android Application](https://play.google.com/store/apps/details?id=com.moonfleet.rxmarbles) 12 | [ReactiveX](http://reactivex.io/) 13 | [ReactiveX Operators](http://reactivex.io/documentation/operators.html) 14 | [RxMarbles](http://rxmarbles.com/) 15 | [RxJava Wiki](https://github.com/ReactiveX/RxJava/wiki) 16 | 17 | ### Dependencies 18 | [RxJava2](https://github.com/ReactiveX/RxJava) 19 | [RxJava2Extensions](https://github.com/akarnokd/RxJava2Extensions) 20 | 21 | ## [Transforming](https://github.com/tunjos/RxJava2-RxMarbles-Samples/blob/master/Transforming/src/Main.java) 22 | 23 | 24 | 25 | ## [Filtering](https://github.com/tunjos/RxJava2-RxMarbles-Samples/blob/master/Filtering/src/Main.java) 26 | 27 | 28 | 29 | ## [Combining](https://github.com/tunjos/RxJava2-RxMarbles-Samples/blob/master/Combining/src/Main.java) 30 | 31 | 32 | 33 | ## [Error Handling](https://github.com/tunjos/RxJava2-RxMarbles-Samples/blob/master/ErrorHandling/src/Main.java) 34 | 35 | 36 | 37 | ## [Conditional](https://github.com/tunjos/RxJava2-RxMarbles-Samples/blob/master/Conditional/src/Main.java) 38 | 39 | 40 | 41 | ## [Math](https://github.com/tunjos/RxJava2-RxMarbles-Samples/blob/master/Math/src/Main.java) 42 | 43 | 44 | 45 | NB 46 | -------- 47 | 48 | All screenshots taken directly from the [RxMarbles Android Application](https://play.google.com/store/apps/details?id=com.moonfleet.rxmarbles). 49 | 50 | License 51 | -------- 52 | 53 | Copyright 2017 Tunji Olu-Taiwo 54 | 55 | Licensed under the Apache License, Version 2.0 (the "License"); 56 | you may not use this file except in compliance with the License. 57 | You may obtain a copy of the License at 58 | 59 | http://www.apache.org/licenses/LICENSE-2.0 60 | 61 | Unless required by applicable law or agreed to in writing, software 62 | distributed under the License is distributed on an "AS IS" BASIS, 63 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 64 | See the License for the specific language governing permissions and 65 | limitations under the License. 66 | -------------------------------------------------------------------------------- /Conditional/src/Main.java: -------------------------------------------------------------------------------- 1 | import com.tunjos.rxjava2.NumberUtils; 2 | import io.reactivex.Observable; 3 | 4 | import java.util.Arrays; 5 | import java.util.Collections; 6 | import java.util.List; 7 | 8 | 9 | public class Main { 10 | public static void main( String[] args ) { 11 | all(); 12 | contains(5); 13 | skipWhile(); 14 | skipUntil(); 15 | takeWhile(); 16 | takeUntil(); 17 | } 18 | 19 | /** 20 | * Emit true if all items emitted by an Observable meet some criteria, or false 21 | * otherwise. 22 | **/ 23 | private static void all() { 24 | System.out.println("all"); 25 | 26 | List integerList = Arrays.asList(3, 5, 7); 27 | System.out.println(integerList); 28 | Observable integerObservable = Observable.fromIterable(integerList); 29 | 30 | System.out.println("all(x => isPrime(x) =====================>"); 31 | 32 | integerObservable.all(NumberUtils::isPrime) 33 | .subscribe(System.out::println); 34 | System.out.println(); 35 | } 36 | 37 | /** 38 | * Emit true if an Observable emits a particular item, or false otherwise. 39 | * 40 | **/ 41 | private static void contains(Object element) { 42 | System.out.println("contains"); 43 | 44 | List integerList = Arrays.asList(1, 55, 25); 45 | System.out.println(integerList); 46 | Observable integerObservable = Observable.fromIterable(integerList); 47 | 48 | System.out.println("contains(" + element + ") =====================>"); 49 | 50 | integerObservable.contains(element) 51 | .subscribe(System.out::println); 52 | System.out.println(); 53 | } 54 | 55 | /** 56 | * Discard items emitted by an Observable until a specified condition becomes false. 57 | * 58 | **/ 59 | private static void skipWhile() { 60 | System.out.println("skipWhile"); 61 | 62 | List integerList = Arrays.asList(1, 2, 3); 63 | System.out.println(integerList); 64 | Observable integerObservable = Observable.fromIterable(integerList); 65 | 66 | System.out.println("skipWhile(x => x != 2) =====================>"); 67 | 68 | integerObservable.skipWhile(x -> x != 2) 69 | .subscribe(System.out::println); 70 | System.out.println(); 71 | } 72 | 73 | /** 74 | * Discard items emitted by an Observable until a second Observable starts emitting 75 | * items. 76 | **/ 77 | private static void skipUntil() { 78 | System.out.println("skipUntil"); 79 | 80 | List integerList = Arrays.asList(1, 2, 3); 81 | List integerList1 = Collections.singletonList(0); 82 | System.out.println(integerList); 83 | System.out.println(integerList1); 84 | Observable integerObservable = Observable.fromIterable(integerList); 85 | Observable integerObservable1 = Observable.fromIterable(integerList1); 86 | 87 | System.out.println("skipUntil =====================>"); 88 | 89 | integerObservable.skipUntil(integerObservable1) 90 | .subscribe(System.out::println); 91 | System.out.println(); 92 | } 93 | 94 | /** 95 | * Mirror items emitted by an Observable until a specified condition becomes false. 96 | * 97 | **/ 98 | private static void takeWhile() { 99 | System.out.println("takeWhile"); 100 | 101 | List integerList = Arrays.asList(2, 4, 16, 9, 11); 102 | System.out.println(integerList); 103 | Observable integerObservable = Observable.fromIterable(integerList); 104 | 105 | System.out.println("takeWhile(x => isEven(x) =====================>"); 106 | 107 | integerObservable.takeWhile(NumberUtils::isEven) 108 | .subscribe(System.out::println); 109 | System.out.println(); 110 | } 111 | 112 | /** 113 | * Discard any items emitted by an Observable after a second Observable emits an 114 | * item or terminates. 115 | **/ 116 | private static void takeUntil() { 117 | System.out.println("takeUntil"); 118 | 119 | List integerList = Arrays.asList(1, 2, 3, 4); 120 | List integerList1 = Collections.singletonList(0); 121 | System.out.println(integerList); 122 | System.out.println(integerList1); 123 | Observable integerObservable = Observable.fromIterable(integerList); 124 | Observable integerObservable1 = Observable.fromIterable(integerList1); 125 | 126 | System.out.println("takeUntil =====================>"); 127 | 128 | integerObservable.takeUntil(integerObservable1) 129 | .subscribe(System.out::println); 130 | System.out.println(); 131 | } 132 | } -------------------------------------------------------------------------------- /Combining/src/Main.java: -------------------------------------------------------------------------------- 1 | import io.reactivex.Observable; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | 7 | public class Main { 8 | public static void main( String[] args ) { 9 | startWith(0); 10 | amb(); 11 | combineLatest(); 12 | concat(); 13 | merge(); 14 | sequenceEqual(); 15 | zip(); 16 | } 17 | 18 | /** 19 | * Emit a specified sequence of items before beginning to emit the items the items 20 | * from the source Observable. 21 | **/ 22 | private static void startWith(Integer item) { 23 | System.out.println("startWith"); 24 | 25 | List integerList = Arrays.asList(1, 2, 3); 26 | System.out.println(integerList); 27 | Observable integerObservable = Observable.fromIterable(integerList); 28 | 29 | System.out.println("startWith(" + item + ") =====================>"); 30 | 31 | integerObservable.startWith(item) 32 | .subscribe(System.out::println); 33 | System.out.println(); 34 | } 35 | 36 | /** 37 | * Given two or more source Observables, emit all of the items from only the first 38 | * of these Observables to emit an item or notification. 39 | **/ 40 | private static void amb() { 41 | System.out.println("amb"); 42 | 43 | List integerList = Arrays.asList(1, 2, 3); 44 | List stringList = Arrays.asList("A", "B", "C"); 45 | System.out.println(integerList); 46 | System.out.println(stringList); 47 | Observable integerObservable = Observable.fromIterable(integerList); 48 | Observable stringObservable = Observable.fromIterable(stringList); 49 | 50 | System.out.println("amb =====================>"); 51 | 52 | Observable.ambArray(integerObservable, stringObservable) 53 | .subscribe(System.out::println); 54 | System.out.println(); 55 | } 56 | 57 | /** 58 | * When an item is emitted by either of two Observables, combine the latest item 59 | * emitted by each Observable via a specified function and emit items based on the 60 | * results of this function. Implemented differently across platforms and 61 | * synchronisation modes. 62 | **/ 63 | private static void combineLatest() { 64 | System.out.println("combineLatest"); 65 | 66 | List integerList = Arrays.asList(1, 2, 3); 67 | List stringList = Arrays.asList("A", "B", "C"); 68 | System.out.println(integerList); 69 | System.out.println(stringList); 70 | Observable integerObservable = Observable.fromIterable(integerList); 71 | Observable stringObservable = Observable.fromIterable(stringList); 72 | 73 | System.out.println("combineLatest =====================>"); 74 | 75 | Observable.combineLatest(integerObservable, stringObservable, (integer, s) -> integer + s) 76 | .subscribe(System.out::println); 77 | System.out.println(); 78 | } 79 | 80 | /** 81 | * Emit the emissions from two or more observables sequentially, one set after 82 | * another. 83 | **/ 84 | private static void concat() { 85 | System.out.println("concat"); 86 | 87 | List integerList = Arrays.asList(1, 2, 3, 4); 88 | List stringList = Arrays.asList("A", "B", "C"); 89 | System.out.println(integerList); 90 | System.out.println(stringList); 91 | Observable integerObservable = Observable.fromIterable(integerList); 92 | Observable stringObservable = Observable.fromIterable(stringList); 93 | 94 | System.out.println("concat =====================>"); 95 | 96 | Observable.concat(integerObservable, stringObservable) 97 | .subscribe(System.out::println); 98 | System.out.println(); 99 | } 100 | 101 | /** 102 | * Combine multiple Observables into one by merging their emissions. 103 | * 104 | **/ 105 | private static void merge() { 106 | System.out.println("merge"); 107 | 108 | List stringList = Arrays.asList("M", "R", "G", "D"); 109 | List stringList1 = Arrays.asList("E", "E"); 110 | System.out.println(stringList); 111 | System.out.println(stringList1); 112 | Observable stringObservable = Observable.fromIterable(stringList); 113 | Observable stringObservable1 = Observable.fromIterable(stringList1); 114 | 115 | System.out.println("merge =====================>"); 116 | 117 | Observable.merge(stringObservable, stringObservable1) 118 | .subscribe(System.out::println); 119 | System.out.println(); 120 | } 121 | 122 | /** 123 | * Emit true if two Observables emit the same sequence of items, or false otherwise. 124 | * 125 | **/ 126 | private static void sequenceEqual() { 127 | System.out.println("sequenceEqual"); 128 | 129 | List integerList = Arrays.asList(1, 2); 130 | List integerList1 = Arrays.asList(1, 2); 131 | System.out.println(integerList); 132 | System.out.println(integerList1); 133 | Observable integerObservable = Observable.fromIterable(integerList); 134 | Observable integerObservable1 = Observable.fromIterable(integerList1); 135 | 136 | System.out.println("sequenceEqual =====================>"); 137 | 138 | Observable.sequenceEqual(integerObservable, integerObservable1) 139 | .subscribe(System.out::println); 140 | System.out.println(); 141 | } 142 | 143 | 144 | /** 145 | * Combine the emissions of multiple Observables together via a specified function 146 | * and emit single items for each combination based on the results of this function. 147 | **/ 148 | private static void zip() { 149 | System.out.println("zip"); 150 | 151 | List integerList = Arrays.asList(1, 2, 3); 152 | List stringList = Arrays.asList("A", "B", "C"); 153 | System.out.println(integerList); 154 | System.out.println(stringList); 155 | Observable integerObservable = Observable.fromIterable(integerList); 156 | Observable stringObservable = Observable.fromIterable(stringList); 157 | 158 | System.out.println("zip =====================>"); 159 | 160 | Observable.zip(integerObservable, stringObservable, (integer, s) -> integer + s) 161 | .subscribe(System.out::println); 162 | System.out.println(); 163 | } 164 | } -------------------------------------------------------------------------------- /Filtering/src/Main.java: -------------------------------------------------------------------------------- 1 | import com.tunjos.rxjava2.NumberUtils; 2 | import io.reactivex.Observable; 3 | 4 | import java.util.Arrays; 5 | import java.util.List; 6 | import java.util.concurrent.TimeUnit; 7 | 8 | 9 | public class Main { 10 | public static void main( String[] args ) { 11 | debounce(1); 12 | distinct(); 13 | distinctUntilChanged(); 14 | elementAt(3); 15 | filter(); 16 | first(); 17 | last(); 18 | skip(2); 19 | skipLast(1); 20 | take(2); 21 | takeLast(2); 22 | ignoreElements(); 23 | } 24 | 25 | /** 26 | * Only emit an item from an Observable if a particular timespan has passed without 27 | * emitting intermediate items. 28 | **/ 29 | private static void debounce(long timeout) { 30 | System.out.println("debounce"); 31 | 32 | List integerList = Arrays.asList(1, 2, 3); 33 | System.out.println(integerList); 34 | Observable integerObservable = Observable.fromIterable(integerList); 35 | 36 | System.out.println("debounce(" + timeout + "ns) =====================>"); 37 | 38 | integerObservable.debounce(timeout, TimeUnit.NANOSECONDS) 39 | .subscribe(System.out::println); 40 | System.out.println(); 41 | } 42 | 43 | /** 44 | * Suppress duplicate items emitted by an Observable. 45 | * 46 | **/ 47 | private static void distinct() { 48 | System.out.println("distinct"); 49 | 50 | List integerList = Arrays.asList(1, 2, 2); 51 | System.out.println(integerList); 52 | Observable integerObservable = Observable.fromIterable(integerList); 53 | 54 | System.out.println("distinct =====================>"); 55 | 56 | integerObservable.distinct() 57 | .subscribe(System.out::println); 58 | System.out.println(); 59 | } 60 | 61 | /** 62 | * Suppress duplicate items emitted by an Observable only until a distinct item is 63 | * emitted. 64 | **/ 65 | private static void distinctUntilChanged() { 66 | System.out.println("distinctUntilChanged"); 67 | 68 | List integerList = Arrays.asList(1, 2, 2, 4, 2); 69 | System.out.println(integerList); 70 | Observable integerObservable = Observable.fromIterable(integerList); 71 | 72 | System.out.println("distinctUntilChanged =====================>"); 73 | 74 | integerObservable.distinctUntilChanged() 75 | .subscribe(System.out::println); 76 | System.out.println(); 77 | } 78 | 79 | /** 80 | * Emit only item N emitted by an Observable. 81 | * 82 | **/ 83 | private static void elementAt(long index) { 84 | System.out.println("elementAt"); 85 | 86 | List integerList = Arrays.asList(1, 2, 3, 4, 5); 87 | System.out.println(integerList); 88 | Observable integerObservable = Observable.fromIterable(integerList); 89 | 90 | System.out.println("elementAt(" + index + ") =====================>"); 91 | 92 | integerObservable.elementAt(index) 93 | .subscribe(System.out::println); 94 | System.out.println(); 95 | } 96 | 97 | /** 98 | * Emit only those items from an Observable that pass a predicate test. 99 | * 100 | **/ 101 | private static void filter() { 102 | System.out.println("filter"); 103 | 104 | List integerList = Arrays.asList(1, 2, 3); 105 | System.out.println(integerList); 106 | Observable integerObservable = Observable.fromIterable(integerList); 107 | 108 | System.out.println("filter(x => isEven(x^2)) =====================>"); 109 | 110 | integerObservable.filter(x -> NumberUtils.isEven(x^2)) 111 | .subscribe(System.out::println); 112 | System.out.println(); 113 | } 114 | 115 | /** 116 | * Emit only the first item, or the first item that meets a condition from an 117 | * Observable. 118 | **/ 119 | private static void first() { 120 | System.out.println("first"); 121 | 122 | List integerList = Arrays.asList(11, 33, 66); 123 | System.out.println(integerList); 124 | Observable integerObservable = Observable.fromIterable(integerList); 125 | 126 | System.out.println("first =====================>"); 127 | 128 | integerObservable.firstElement() 129 | .subscribe(System.out::println); 130 | System.out.println(); 131 | } 132 | 133 | /** 134 | * Emit only the last item emitted by an Observable. 135 | * 136 | **/ 137 | private static void last() { 138 | System.out.println("last"); 139 | 140 | List integerList = Arrays.asList(11, 33, 66); 141 | System.out.println(integerList); 142 | Observable integerObservable = Observable.fromIterable(integerList); 143 | 144 | System.out.println("last =====================>"); 145 | 146 | integerObservable.lastElement() 147 | .subscribe(System.out::println); 148 | System.out.println(); 149 | } 150 | 151 | /** 152 | * Suppress the first N items emitted by an Observable. 153 | * 154 | **/ 155 | private static void skip(long count) { 156 | System.out.println("skip"); 157 | 158 | List integerList = Arrays.asList(1, 2, 3); 159 | System.out.println(integerList); 160 | Observable integerObservable = Observable.fromIterable(integerList); 161 | 162 | System.out.println("skip(" + count + ") =====================>"); 163 | 164 | integerObservable.skip(count) 165 | .subscribe(System.out::println); 166 | System.out.println(); 167 | 168 | } 169 | 170 | /** 171 | * Suppress the last N items emitted by an Observable. 172 | * 173 | **/ 174 | private static void skipLast(int count) { 175 | System.out.println("skipLast"); 176 | 177 | List stringList = Arrays.asList("A", "B", "C"); 178 | System.out.println(stringList); 179 | Observable integerObservable = Observable.fromIterable(stringList); 180 | 181 | System.out.println("skipLast(" + count + ") =====================>"); 182 | 183 | integerObservable.skipLast(count) 184 | .subscribe(System.out::println); 185 | System.out.println(); 186 | 187 | } 188 | 189 | /** 190 | * Emit only the first N items emitted by an Observable. 191 | * 192 | **/ 193 | private static void take(long count) { 194 | System.out.println("take"); 195 | 196 | List integerList = Arrays.asList(1, 10, 15); 197 | System.out.println(integerList); 198 | Observable integerObservable = Observable.fromIterable(integerList); 199 | 200 | System.out.println("take(" + count + ") =====================>"); 201 | 202 | integerObservable.take(count) 203 | .subscribe(System.out::println); 204 | System.out.println(); 205 | 206 | } 207 | 208 | /** 209 | * Emit only the last N items emitted by an Observable. 210 | * 211 | **/ 212 | private static void takeLast(int count) { 213 | System.out.println("takeLast"); 214 | 215 | List stringList = Arrays.asList("A", "B", "C"); 216 | System.out.println(stringList); 217 | Observable integerObservable = Observable.fromIterable(stringList); 218 | 219 | System.out.println("takeLast(" + count + ") =====================>"); 220 | 221 | integerObservable.takeLast(count) 222 | .subscribe(System.out::println); 223 | System.out.println(); 224 | 225 | } 226 | 227 | /** 228 | * Do not emit any items from an Observable but mirror its termination notification. 229 | * 230 | **/ 231 | private static void ignoreElements() { 232 | System.out.println("ignoreElements"); 233 | 234 | List integerList = Arrays.asList(1, 2, 3); 235 | System.out.println(integerList); 236 | Observable integerObservable = Observable.fromIterable(integerList); 237 | 238 | System.out.println("ignoreElements =====================>"); 239 | 240 | integerObservable.ignoreElements() 241 | .subscribe(System.out::println); 242 | System.out.println(); 243 | } 244 | } --------------------------------------------------------------------------------