Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava 2 equivalent of Observable.from(Iterable).firstOrDefault?

I have the following in RxJava 1 which returns an Observable<SomeObject>:

// objects is a List<SomeObject>
return rx.Observable.from(objects)
                                .firstOrDefault(aDefaultObject, new Func1<SomeObject, Boolean>() {
                                    @Override
                                    public Boolean call(SomeObject aObject) {
                                        return aObject.isSomething();
                                    }
                                });

My goal is to return a io.reactivex.Single<SomeObject> (RxJava 2) instead.
How can I achieve that?

I came up with something as follows, is it correct?:

return Flowable.fromIterable(objects).filter(new Predicate<SomeObject>() {
                            @Override
                            public boolean test(SomeObject aObject) throws Exception {
                                return aObject.isSomething();
                            }
                        }).first(aDefaultObject);
like image 813
ericn Avatar asked Dec 19 '25 22:12

ericn


1 Answers

Thanks to @marianosimone, correct solution is:

return io.reactivex.Observable.fromIterable(objects).filter(new Predicate<SomeObject>() {
                            @Override
                            public boolean test(SomeObject aObject) throws Exception {
                                return aObject.isSomething();
                            }
                        }).first(aDefaultObject);

Flowable also works but it's not recommended by official docs:
When to use Observable
- You have a flow of no more than 1000 elements at its longest: i.e., you have so few elements over time that there is practically no chance for OOME in your application.
- You deal with GUI events such as mouse moves or touch events: these can rarely be backpressured reasonably and aren't that frequent. You may be able to handle an element frequency of 1000 Hz or less with Observable but consider using sampling/debouncing anyway.
- Your flow is essentially synchronous but your platform doesn't support Java Streams or you miss features from it. Using Observable has lower overhead in general than Flowable. (You could also consider IxJava which is optimized for Iterable flows supporting Java 6+).
When to use Flowable
- Dealing with 10k+ of elements that are generated in some fashion somewhere and thus the chain can tell the source to limit the amount it generates.
- Reading (parsing) files from disk is inherently blocking and pull-based which works well with backpressure as you control, for example, how many lines you read from this for a specified request amount).
- Reading from a database through JDBC is also blocking and pull-based and is controlled by you by calling ResultSet.next() for likely each downstream request.
- Network (Streaming) IO where either the network helps or the protocol used supports requesting some logical amount.
- Many blocking and/or pull-based data sources which may eventually get a non-blocking reactive API/driver in the future.

like image 147
ericn Avatar answered Dec 23 '25 19:12

ericn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!