A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://reactivex.io/documentation/operators/last.html below:

ReactiveX - Last operator

In RxJava, this filtering operator is implemented as last and lastOrDefault.

Somewhat confusingly, there are also BlockingObservable operators called last and lastOrDefault that block and then return items, rather than immediately returning Observables.

The Filtering Operators

To filter an Observable so that only its last emission is emitted, use the last operator with no parameters.

Sample Code
Observable.just(1, 2, 3)
          .last()
          .subscribe(new Subscriber<Integer>() {
        @Override
        public void onNext(Integer item) {
            System.out.println("Next: " + item);
        }

        @Override
        public void onError(Throwable error) {
            System.err.println("Error: " + error.getMessage());
        }

        @Override
        public void onCompleted() {
            System.out.println("Sequence complete.");
        }
    });
Next: 3
Sequence complete.

You can also pass a predicate function to last, in which case it will produce an Observable that emits only the last item from the source Observable that the predicate evaluates as true.

The lastOrDefault operator is similar to last, but you pass it a default item that it can emit if the source Observable fails to emit any items.

lastOrDefault also has a variant to which you can pass a predicate function, so that its Observable will emit the last item from the source Observable that the predicate evaluates as true, or the default item if no items emitted by the source Observable pass the predicate.

last and lastOrDefault do not by default operate on any particular Scheduler.

The BlockingObservable Methods

The BlockingObservable methods do not transform an Observable into another, filtered Observable, but rather they break out of the Observable cascade, blocking until the Observable emits the desired item, and then return that item itself.

To turn an Observable into a BlockingObservable so that you can use these methods, you can use either the Observable.toBlocking or BlockingObservable.from methods.

To retrieve the last emission from a BlockingObservable, use the last method with no parameters.

You can also pass a predicate function to the last method to retrieve the last emission from a BlockingObservable that satisfies the predicate.

As with the filtering operators, the last method of BlockingObservable will throw a NoSuchElementException if there is no last element in the source BlockingObservable. To return a default item instead in such cases, use the lastOrDefault method.

And, as with last, there is a lastOrDefault variant that takes a predicate function as an argument and retrieves the last item from the source BlockingObservable that satisfies that predicate, or a default item instead if no satisfying item was emitted.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4