A RetroSearch Logo

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

Search Query:

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

ReactiveX - First operator

In RxJava, this filtering operator is implemented as first, firstOrDefault, and takeFirst.

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

There are also several other operators that perform similar functions.

The Filtering Operators

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

Sample Code
Observable.just(1, 2, 3)
          .first()
          .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: 1
Sequence complete.

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

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

firstOrDefault also has a variant to which you can pass a predicate function, so that its Observable will emit the first 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.

The takeFirst operator behaves similarly to first, with the exception of how these operators behave when the source Observable emits no items that satisfy the predicate. In such a case, first will throw a NoSuchElementException while takeFirst will return an empty Observable (one that calls onCompleted but never calls onNext).

The single operator is similar to first, but throws a NoSuchElementException if the source Observable does not emit exactly one item before successfully completing.

single also has a version that accepts a predicate, and emits the sole item emitted by the source Observable that matches that predicate, or notifies of an exception if exactly one such item does not match.

As with firstOrDefault there is also a singleOrDefault that emits a default item if the source Observable is empty, although it will still notify of an error if the source Observable emits more than one item.

And there is also a version of singleOrDefault that takes a predicate function and emits the sole item from the source Observable that matches that predicate, if any; the default item if no such items match; and makes an error notification if multiple items match.

first, firstOrDefault, single, singleOrDefault, and takeFirst 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 first emission from a BlockingObservable, use the first method with no parameters.

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

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

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

The single operator is similar to first, but throws a NoSuchElementException if the source Observable does not emit exactly one item before successfully completing.

single also has a version that accepts a predicate, and returns the sole item emitted by the source Observable that matches that predicate, or throws an exception if exactly one such item does not match.

As with firstOrDefault there is also a singleOrDefault that returns a default item if the source Observable is empty, although it will still throw an error if the source Observable emits more than one item.

And there is also a version of singleOrDefault that takes a predicate function and returns the sole item from the source Observable that matches that predicate, if any; the default item if no such items match; and throws an error if multiple items match.

The next operator blocks until the BlockingObservable emits another item, and then returns that item. You can call this function repeatedly to get successive items from the BlockingObservable, effectively iterating over its emissions in a blocking fashion.

The latest operator is similar, but rather than blocking to wait for the next emitted item, it immediately returns the most-recently-emitted item, and only blocks if the Observable has not yet emitted anything.

The mostRecent operator similarly allows you to iterate over the emissions of a BlockingObservable, but its Iterable always immediately returns a value: either a default item you provide (if the BlockingObservable has not yet emitted an item), or the latest item the BlockingObservable has 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