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
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:
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With