Thursday, 18 January 2018

Operator - first




emit only the first item (or the first item that meets some condition) emitted by an Observable


If you are only interested in the first item emitted by an Observable, or the first item that meets some criteria, you can filter the Observable with the First operator.
In some implementations, First is not implemented as a filtering operator that returns an Observable, but as a blocking function that returns a particular item at such time as the source Observable emits that item. In those implementations, if you instead want a filtering operator, you may have better luck with Take(1) or ElementAt(0).
In some implementations there is also a Single operator. It behaves similarly to First except that it waits until the source Observable terminates in order to guarantee that it only emits a single item (otherwise, rather than emitting that item, it terminates with an error). You can use this to not only take the first item from the source Observable but to also guarantee that there was only one item.

---------------------------------------------------

 Observable
.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.first(0)
.subscribe(System.out::println);


----------------------------------------------------






































22

Operator - Filter



Filter

emit only those items from an Observable that pass a predicate test



The Filter operator filters an Observable by only allowing items through that pass a test that you specify in the form of a predicate function.


-----------------------------------------------------------------


//        EVEN
        Observable
                .just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
                .filter(integer -> integer % 2 == 0)
                .subscribe(System.out::println);

//          ODD
        Observable
                .just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
                .filter(integer -> integer % 2 == 1)
                .subscribe(System.out::println);



----------------------------------------------------------------






















22

Operator - elementAt



ElementAt

emit only item n emitted by an Observable

The ElementAt operator pulls an item located at a specified index location in the sequence of items emitted by the source Observable and emits that item as its own sole emission.



-----------------------------------------------------------------------------

        List<String> list = new ArrayList<>();
        list.add("List-1");
        list.add("List-2");
        list.add("List-3");
        list.add("List-4");
        list.add("List-5");


       Observable.fromIterable(list).elementAt(2).subscribe(System.out::println);

-------------------------------------------------------------------------------------------------------------
or Default

Observable.fromIterable(list).elementAt(15, list.get(1)).subscribe(System.out::println);

----------------------------------------------------------------------------------------------------------------
or Error

Observable.fromIterable(list).elementAtOrError(15).subscribe(System.out::println);


-----------------------------------------------------------------------------




























22

Operator - Do




Do

register an action to take upon a variety of Observable lifecycle events





You can register callbacks that ReactiveX will call when certain events take place on an Observable, where those callbacks will be called independently from the normal set of notifications associated with an Observable cascade. There are a variety of operators that various ReactiveX implementations have designed to allow for this.


RxJava 2․x doAfterTerminate doOnComplete doOnDispose doOnEach doOnError doOnLifecycle doOnNext doOnSubscribe doOnTerminate onTerminateDetach


--------------------------------------------------------------------------------------------


       Observable.just("Hello").doOnNext(new Consumer<String>() {
                   @Override
                   public void accept(String s) throws Exception {
                       System.out.println("Consumer Test");
                   }
               })
                .doOnEach(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable disposable) {

                    }

                    @Override
                    public void onNext(String s) {
                        System.out.println("Observer Test");
                    }

                    @Override
                    public void onError(Throwable throwable) {

                    }

                    @Override
                    public void onComplete() {

                    }
                })
               .doOnComplete(new Action() {
                   @Override
                   public void run() throws Exception {
                       System.out.println("Action Test");
                   }
               })

               .subscribe();


--------------------------------------------------------------------------------------------






































22

Operator - Distinct



Distinct

suppress duplicate items emitted by an Observable






The Distinct operator filters an Observable by only allowing items through that have not already been emitted.
In some implementations there are variants that allow you to adjust the criteria by which two items are considered “distinct.” In some, there is a variant of the operator that only compares an item against its immediate predecessor for distinctness, thereby filtering only consecutive duplicate items from the sequence.

-------------------------------------------------------------------------

Observable.just(1,2,3,3,4,5,5).distinct().subscribe(System.out::println);

------------------------------------------------------------------------

















22

Operator - Delay




Delay

shift the emissions from an Observable forward in time by a particular amount




The Delay operator modifies its source Observable by pausing for a particular increment of time (that you specify) before emitting each of the source Observable’s items. This has the effect of shifting the entire sequence of items emitted by the Observable forward in time by that specified increment.

-----------------------------------------------------

Observable
fromIterable(list)
.toList()
.delay(50, TimeUnit.MILLISECONDS)
.subscribe(System.out::println);

----------------------------------------------------


























22

Operator - defaultIfEmpty



DefaultIfEmpty

emit items from the source Observable, or a default item if the source Observable emits nothing




The DefaultIfEmpty operator simply mirrors the source Observable exactly if the source Observable emits any items. If the source Observable terminates normally (with an onComplete) without emitting any items, the Observable returned from DefaultIfEmpty will instead emit a default item of your choosing before it too completes.


----------------------------------------------------

Observable.empty()
                .defaultIfEmpty(0)
                .subscribe(v -> System.out.println(v));

---------------------------------------------------











22

Calling method Sequencely

1. Execute multiple task sequencly (WAY-1) ------------------------------------------------------------------------------ import io.re...