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).
TBD
TBD
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.
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.
last(Func1)
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(T)
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.
lastOrDefault(T)
last and lastOrDefault do not by default operate on any particular
Scheduler.
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.
Observable.toBlocking()BlockingObservable.from(Observable)
To retrieve the last emission from a BlockingObservable, use the last method
with no parameters.
BlockingObservable.last()
You can also pass a predicate function to the last method to retrieve the last emission
from a BlockingObservable that satisfies the predicate.
BlockingObservable.last(Func1)
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.
BlockingObservable.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.
To filter an Observable so that only its last emission is emitted, use the
last operator with no parameters.
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.
last(Func1)
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(T)
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.
lastOrDefault(T)
last and lastOrDefault do not by default operate on any particular
Scheduler.
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.
Observable.toBlocking()BlockingObservable.from(Observable)
To retrieve the last emission from a BlockingObservable, use the last method
with no parameters.
BlockingObservable.last()
You can also pass a predicate function to the last method to retrieve the last emission
from a BlockingObservable that satisfies the predicate.
BlockingObservable.last(Func1)
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.
BlockingObservable.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.
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:
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:
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.jsrx.all.compat.jsrx.aggregates.jsIt requires one of the following distributions:
rx.jsrx.compat.jsrx.lite.jsrx.lite.compat.js