Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter list of Object in Rxjava

I want to filter List<Object> based on query from user and then return List<Object> to him/her.I found out how to filter items But the problem is I don't know how return List<Object>. I also see some approach which iterate and call flatMap each time But I didn't think it's an elegant way.

This is my last attempt:

      Observable.from(my_list_of_object)
                    .debounce(500, TimeUnit.MILLISECONDS)
                    .filter(new Func1<MyObject, Boolean>() {
                        @Override
                        public Boolean call(MyObject o) {
                            return o.getName().contains(query); //filtering
                        }
                    })
                    .observeOn(Schedulers.computation())

                   //problem is here and I dont know how 
                   //to convert filtered Item to list
like image 368
Amir Avatar asked Jun 30 '16 09:06

Amir


People also ask

How to filter observables in RxJava?

For this purpose, RxJava offers various filtering capabilities. Let's start looking at the filter method. 2.1. The filter Operator Simply put, the filter operator filters an Observable making sure that emitted items match specified condition, which comes in the form of a Predicate. Let's see how we can filter only the odd values from those emitted:

What is RxJava’s observable emits an item from an observable?

This article is part of RxJava Introduction series. You can checkout the entire series here: This operator only emits an item from an Observable if a particular timespan has passed without it emitting another item.

What is filtering a collection by a list?

Filtering a Collection by a List is a common business logic scenario. There are plenty of ways to achieve this. However, some may lead to under-performing solutions if not done properly. In this tutorial, we'll compare some filtering implementations and discuss their advantages and drawbacks. 2.

What does the filter condition do in observable?

This operator emits only those items from an Observable that pass a predicate test. Sample Implementation: In the below code, for a list of integers from 1 to 6, we add a filter condition that filters only the even numbers and emits those integers.


1 Answers

Just use toList() operator. Check the documentation.

      Observable.from(my_list_of_object)
                    .debounce(500, TimeUnit.MILLISECONDS)
                    .filter(new Func1<MyObject, Boolean>() {
                        @Override
                        public Boolean call(MyObject o) {
                            return o.getName().contains(query); //filtering
                        }
                    })
                    .toList()
                    .observeOn(Schedulers.computation())

You can find more extensive list of aggregate operators here.

like image 67
MatBos Avatar answered Oct 15 '22 18:10

MatBos