Last

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

If you are only interested in the last item emitted by an Observable, or the last item that meets some criteria, you can filter the Observable with the Last operator.

In some implementations, Last is not implemented as a filtering operator that returns an Observable, but as a blocking function that returns a particular item when the source Observable terminates. In those implementations, if you instead want a filtering operator, you may have better luck with TakeLast(1).

See Also

Language-Specific Information:

In RxGroovy, 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

last

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

last

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.

lastOrDefault

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

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.

last

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

last

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

lastOrDefault

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.

lastOrDefault

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.

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

last

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.
last

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.

lastOrDefault

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

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.

last

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

last

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

lastOrDefault

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.

lastOrDefault

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.

last

RxJS implements the last operator. It optionally takes a predicate function as a parameter, in which case, rather than emitting the last item from the source Observable, the resulting Observable will emit the last item from the source Observable that satisfies the predicate.

The predicate function itself takes three arguments:

  1. the item from the source Observable to be, or not be, filtered
  2. the zero-based index of this item in the source Observable’s sequence
  3. the source Observable object

Sample Code

var source = Rx.Observable.range(0, 10)
    .last(function (x, idx, obs) { return x % 2 === 1; });

var subscription = source.subscribe(
    function (x) { console.log('Next: ' + x); },
    function (err) { console.log('Error: ' + err); },
    function () { console.log('Completed'); });
Next: 8
Completed

If the source Observable emits no items (or no items that match the predicate), last will terminate with a “Sequence contains no elements.onError notification.

If instead you want the Observable to emit a default value in such a case, you can pass a second parameter (named defaultValue) to last:

Sample Code

var source = Rx.Observable.range(0, 10)
    .last(function (x, idx, obs) { return x > 42; }, 88 );

var subscription = source.subscribe(
    function (x) { console.log('Next: ' + x); },
    function (err) { console.log('Error: ' + err); },
    function () { console.log('Completed'); });
Next: 88
Completed

last is found in each of the following distributions:

  • rx.all.js
  • rx.all.compat.js
  • rx.aggregates.js

It requires one of the following distributions:

  • rx.js
  • rx.compat.js
  • rx.lite.js
  • rx.lite.compat.js