RxJava – PublishSubject ”; Previous Next PublishSubject emits items to currently subscribed Observers and terminal events to current or late Observers. Class Declaration Following is the declaration for io.reactivex.subjects.PublishSubject<T> class − public final class PublishSubject<T> extends Subject<T> PublishSubject Example Create the following Java program using any editor of your choice in, say, C:> RxJava. ObservableTester.java import io.reactivex.subjects.PublishSubject; public class ObservableTester { public static void main(String[] args) { final StringBuilder result1 = new StringBuilder(); final StringBuilder result2 = new StringBuilder(); PublishSubject<String> subject = PublishSubject.create(); subject.subscribe(value -> result1.append(value) ); subject.onNext(“a”); subject.onNext(“b”); subject.onNext(“c”); subject.subscribe(value -> result2.append(value)); subject.onNext(“d”); subject.onComplete(); //Output will be abcd System.out.println(result1); //Output will be d only //as subscribed after c item emitted. System.out.println(result2); } } Verify the Result Compile the class using javac compiler as follows − C:RxJava>javac ObservableTester.java Now run the ObservableTester as follows − C:RxJava>java ObservableTester It should produce the following output − abcd d Print Page Previous Next Advertisements ”;
Category: rxjava
RxJava – Combining Operators
RxJava – Combining Operators ”; Previous Next Following are the operators which are used to create a single Observable from multiple Observables. Sr.No. Operator & Description 1 And/Then/When Combine item sets using Pattern and Plan intermediaries. 2 CombineLatest Combine the latest item emitted by each Observable via a specified function and emit resulted item. 3 Join Combine items emitted by two Observables if emitted during time-frame of second Observable emitted item. 4 Merge Combines the items emitted of Observables. 5 StartWith Emit a specified sequence of items before starting to emit the items from the source Observable 6 Switch Emits the most recent items emitted by Observables. 7 Zip Combines items of Observables based on function and emits the resulted items. Combining Operator Example Create the following Java program using any editor of your choice in, say, C:> RxJava. ObservableTester.java import io.reactivex.Observable; //Using combineLatest operator to combine Observables public class ObservableTester { public static void main(String[] args) { Integer[] numbers = { 1, 2, 3, 4, 5, 6}; String[] letters = {“a”, “b”, “c”, “d”, “e”, “f”, “g”}; final StringBuilder result = new StringBuilder(); Observable<String> observable1 = Observable.fromArray(letters); Observable<Integer> observable2 = Observable.fromArray(numbers); Observable.combineLatest(observable1, observable2, (a,b) -> a + b) .subscribe( letter -> result.append(letter)); System.out.println(result); } } Verify the Result Compile the class using javac compiler as follows − C:RxJava>javac ObservableTester.java Now run the ObservableTester as follows − C:RxJava>java ObservableTester It should produce the following output − g1g2g3g4g5g6 Print Page Previous Next Advertisements ”;
RxJava – AsyncSubject
RxJava – AsyncSubject ”; Previous Next AsyncSubject emits the only last value followed by a completion event or the received error to Observers. Class Declaration Following is the declaration for io.reactivex.subjects.AsyncSubject<T> class − public final class AsyncSubject<T> extends Subject<T> AsyncSubject Example Create the following Java program using any editor of your choice in, say, C:> RxJava. ObservableTester.java import io.reactivex.subjects. AsyncSubject; public class ObservableTester { public static void main(String[] args) { final StringBuilder result1 = new StringBuilder(); final StringBuilder result2 = new StringBuilder(); AsyncSubject<String> subject = AsyncSubject.create(); subject.subscribe(value -> result1.append(value) ); subject.onNext(“a”); subject.onNext(“b”); subject.onNext(“c”); subject.subscribe(value -> result2.append(value)); subject.onNext(“d”); subject.onComplete(); //Output will be d being the last item emitted System.out.println(result1); //Output will be d being the last item emitted System.out.println(result2); } } Verify the Result Compile the class using javac compiler as follows − C:RxJava>javac ObservableTester.java Now run the ObservableTester as follows − C:RxJava>java ObservableTester It should produce the following output − d d Print Page Previous Next Advertisements ”;
RxJava – ReplaySubject
RxJava – ReplaySubject ”; Previous Next ReplaySubject replays events/items to current and late Observers. Class Declaration Following is the declaration for io.reactivex.subjects.ReplaySubject<T> class − public final class ReplaySubject<T> extends Subject<T> ReplaySubject Example Create the following Java program using any editor of your choice in, say, C:> RxJava. ObservableTester.java import io.reactivex.subjects.ReplaySubject; public class ObservableTester { public static void main(String[] args) { final StringBuilder result1 = new StringBuilder(); final StringBuilder result2 = new StringBuilder(); ReplaySubject<String> subject = ReplaySubject.create(); subject.subscribe(value -> result1.append(value) ); subject.onNext(“a”); subject.onNext(“b”); subject.onNext(“c”); subject.subscribe(value -> result2.append(value)); subject.onNext(“d”); subject.onComplete(); //Output will be abcd System.out.println(result1); //Output will be abcd being ReplaySubject //as ReplaySubject emits all the items System.out.println(result2); } } Verify the Result Compile the class using javac compiler as follows − C:RxJava>javac ObservableTester.java Now run the ObservableTester as follows − C:RxJava>java ObservableTester It should produce the following output − abcd abcd Print Page Previous Next Advertisements ”;
RxJava – Utility Operators
RxJava – Utility Operators ”; Previous Next Following are the operators which are often useful with Observables. Sr.No. Operator & Description 1 Delay Register action to handle Observable life-cycle events. 2 Materialize/Dematerialize Represents item emitted and notification sent. 3 ObserveOn Specify the scheduler to be observed. 4 Serialize Force Observable to make serialized calls. 5 Subscribe Operate upon the emissions of items and notifications like complete from an Observable 6 SubscribeOn Specify the scheduler to be used by an Observable when it is subscribed to. 7 TimeInterval Convert an Observable to emit indications of the amount of time elapsed between emissions. 8 Timeout Issues error notification if specified time occurs without emitting any item. 9 Timestamp Attach timestamp to each item emitted. 9 Using Creates a disposable resource or same lifespan as that of Observable. Utility Operator Example Create the following Java program using any editor of your choice in, say, C:> RxJava. ObservableTester.java import io.reactivex.Observable; //Using subscribe operator to subscribe to an Observable public class ObservableTester { public static void main(String[] args) { String[] letters = {“a”, “b”, “c”, “d”, “e”, “f”, “g”}; final StringBuilder result = new StringBuilder(); Observable<String> observable = Observable.fromArray(letters); observable.subscribe( letter -> result.append(letter)); System.out.println(result); } } Verify the Result Compile the class using javac compiler as follows − C:RxJava>javac ObservableTester.java Now run the ObservableTester as follows − C:RxJava>java ObservableTester It should produce the following output − abcdefg Print Page Previous Next Advertisements ”;
RxJava – Creating Operators
RxJava – Creating Operators ”; Previous Next Following are the operators which are used to create an Observable. Sr.No. Operator & Description 1 Create Creates an Observable from scratch and allows observer method to call programmatically. 2 Defer Do not create an Observable until an observer subscribes. Creates a fresh observable for each observer. 3 Empty/Never/Throw Creates an Observable with limited behavior. 4 From Converts an object/data structure into an Observable. 5 Interval Creates an Observable emitting integers in sequence with a gap of specified time interval. 6 Just Converts an object/data structure into an Observable to emit the same or same type of objects. 7 Range Creates an Observable emitting integers in sequence of given range. 8 Repeat Creates an Observable emitting integers in sequence repeatedly. 9 Start Creates an Observable to emit the return value of a function. 10 Timer Creates an Observable to emit a single item after given delay. Creating Operator Example Create the following Java program using any editor of your choice in, say, C:> RxJava. ObservableTester.java import io.reactivex.Observable; //Using fromArray operator to create an Observable public class ObservableTester { public static void main(String[] args) { String[] letters = {“a”, “b”, “c”, “d”, “e”, “f”, “g”}; final StringBuilder result = new StringBuilder(); Observable<String> observable = Observable.fromArray(letters); observable .map(String::toUpperCase) .subscribe( letter -> result.append(letter)); System.out.println(result); } } Verify the Result Compile the class using javac compiler as follows − C:RxJava>javac ObservableTester.java Now run the ObservableTester as follows − C:RxJava>java ObservableTester It should produce the following output − ABCDEFG Print Page Previous Next Advertisements ”;
RxJava – Single Observable
RxJava – Single Observable ”; Previous Next The Single class represents the single value response. Single observable can only emit either a single successful value or an error. It does not emit onComplete event. Class Declaration Following is the declaration for io.reactivex.Single<T> class − public abstract class Single<T> extends Object implements SingleSource<T> Protocol Following is the sequential protocol that Single Observable operates − onSubscribe (onSuccess | onError)? Single Example Create the following Java program using any editor of your choice in, say, C:> RxJava. ObservableTester.java import java.util.concurrent.TimeUnit; import io.reactivex.Single; import io.reactivex.disposables.Disposable; import io.reactivex.observers.DisposableSingleObserver; import io.reactivex.schedulers.Schedulers; public class ObservableTester { public static void main(String[] args) throws InterruptedException { //Create the observable Single<String> testSingle = Single.just(“Hello World”); //Create an observer Disposable disposable = testSingle .delay(2, TimeUnit.SECONDS, Schedulers.io()) .subscribeWith( new DisposableSingleObserver<String>() { @Override public void onError(Throwable e) { e.printStackTrace(); } @Override public void onSuccess(String value) { System.out.println(value); } }); Thread.sleep(3000); //start observing disposable.dispose(); } } Verify the Result Compile the class using javac compiler as follows − C:RxJava>javac ObservableTester.java Now run the ObservableTester as follows − C:RxJava>java ObservableTester It should produce the following output − Hello World Print Page Previous Next Advertisements ”;
RxJava – Home
RxJava Tutorial PDF Version Quick Guide Resources Job Search Discussion RxJava is a Java based extension of ReactiveX. ReactiveX is a project which aims to provide reactive programming concept to various programming languages. Reactive Programming refers to the scenario where program reacts as and when data appears. It is a event based programming concept and events can propagate to registers observers. As per the Reactive, they have combined the best of Observer pattern, Iterator pattern and functional pattern. The Observer pattern done right. ReactiveX is a combination of the best ideas from the Observer pattern, the Iterator pattern, and functional programming. Audience This tutorial has been designed for all those readers who want to learn the features of RxJava. This tutorial covers most of the topics required for a basic understanding of RxJava and to get a feel of how it works. Prerequisites This tutorial has been prepared for the beginners to help them understand the basic to advanced concepts related to RxJava. Print Page Previous Next Advertisements ”;
RxJava – Mathematical Operators ”; Previous Next Following are the operators which operates on entire items emitted by an Observable. Sr.No. Operator & Description 1 Average Evaluates averages of all items and emit the result. 2 Concat Emits all items from multiple Observable without interleaving. 3 Count Counts all items and emit the result. 4 Max Evaluates max valued item of all items and emit the result. 5 Min Evaluates min valued item of all items and emit the result. 6 Reduce Apply a function on each item and return the result. 7 Sum Evaluates sum of all items and emit the result. Mathematical Operator Example Create the following Java program using any editor of your choice in, say, C:> RxJava. ObservableTester.java import io.reactivex.Observable; //Using concat operator to operate on multiple Observables public class ObservableTester { public static void main(String[] args) throws InterruptedException { Integer[] numbers = { 1, 2, 3, 4, 5, 6}; String[] letters = {“a”, “b”, “c”, “d”, “e”, “f”, “g”}; final StringBuilder result = new StringBuilder(); Observable<String> observable1 = Observable.fromArray(letters); Observable<Integer> observable2 = Observable.fromArray(numbers); Observable.concat(observable1, observable2) .subscribe( letter -> result.append(letter)); System.out.println(result); } } Verify the Result Compile the class using javac compiler as follows − C:RxJava>javac ObservableTester.java Now run the ObservableTester as follows − C:RxJava>java ObservableTester It should produce the following output − abcdefg123456 Print Page Previous Next Advertisements ”;
RxJava – Transforming Operators ”; Previous Next Following are the operators which are used to transform an item emitted from an Observable. Sr.No. Operator & Description 1 Buffer Gathers items from Observable into bundles periodically and then emit the bundles rather than items. 2 FlatMap Used in nested observables. Transforms items into Observables. Then flatten the items into single Observable. 3 GroupBy Divide an Observable into set of Observables organized by key to emit different group of items. 4 Map Apply a function to each emitted item to transform it. 5 Scan Apply a function to each emitted item, sequentially and then emit the successive value. 6 Window Gathers items from Observable into Observable windows periodically and then emit the windows rather than items. Transforming Operator Example Create the following Java program using any editor of your choice in, say, C:> RxJava. ObservableTester.java import io.reactivex.Observable; //Using map operator to transform an Observable public class ObservableTester { public static void main(String[] args) { String[] letters = {“a”, “b”, “c”, “d”, “e”, “f”, “g”}; final StringBuilder result = new StringBuilder(); Observable<String> observable = Observable.fromArray(letters); observable .map(String::toUpperCase) .subscribe( letter -> result.append(letter)); System.out.println(result); } } Verify the Result Compile the class using javac compiler as follows − C:RxJava>javac ObservableTester.java Now run the ObservableTester as follows − C:RxJava>java ObservableTester It should produce the following output − ABCDEFG Print Page Previous Next Advertisements ”;