In the previous article we introduced the conversion class operator, then this chapter we will introduce the filter class operator. As the name implies, this type of operators is primarily used to filter the event data, returning only the data that satisfies our criteria. The filter class operators mainly include Filter
: Take
TakeLast
TakeUntil
Skip SkipLast
ElementAt
c12> Debounce
and so Distinct
on. DistinctUntilChanged
First
Last
Filter
filter(Func1)
To filter the values we don't want in the observation sequence and return only the values that satisfy the condition, let's look at the schematic:
Filter (FUNC1)
Or take the community in the previous article for Community[] communities
example, suppose I need to race to select all the housing number more than 10 of the community, we can achieve this:
Observable.from(communities) .filter(new Func1<Community, Boolean>() { @Override public Boolean call(Community community) { return community.houses.size()>10; } }).subscribe(new Action1<Community>() { @Override public void call(Community community) { System.out.println(community.name); }});
Take
take(int)
Use an integer n as a parameter to emit the first n elements from the original sequence.
Take (int)
Now we need to take communities
the first 10 quarters in the cell list.
Observable.from(communities) .take(10) .subscribe(new Action1<Community>() { @Override public void call(Community community) { System.out.println(community.name); } });
Takelast
takeLast(int)
It also uses an integer n as the argument, except that it emits the last n elements of the observed sequence.
Takelast (int)
Get communities
the 3 cells in the cell list
Observable.from(communities) .takeLast(3) .subscribe(new Action1<Community>() { @Override public void call(Community community) { System.out.println(community.name); } });
Takeuntil
takeUntil(Observable)
Subscribe and start launching the original observable, while monitoring the second observable we provide. If the second observable launches a data or launches a termination notification, takeUntil()
the returned observable stops transmitting the original observable and terminates.
Takeuntil (Observable)
observable<long> Observablea = Observable.interval (timeunit.milliseconds);observable<long> Observableb = Observable.interval (Timeunit.milliseconds); Observablea.takeuntil (Observableb). Subscribe (New Subscriber<long> () { @Override public void oncompleted () {system.exit (0); } @Override public void onerror (Throwable e) {} @Override public void onnext (Long along) {System.out.println ( Along); } }); try {thread.sleep (integer.max_value);} catch (interruptedexception e) {e.printstacktrace ();
Program output:
01
takeUntil(Func1)
The call method in FUNC1 is used to determine if the transmit data needs to be terminated.
Takeuntil (FUNC1)
Observable.just (1, 2, 3, 4, 5, 6, 7). Takeuntil (new Func1<integer, boolean> () { @Override < Span class= "Hljs-keyword" >public Boolean call (integer integer) { return integer >= 5;}). Subscribe (new action1<integer> () { @Override public void call< Span class= "Hljs-params" (Integer integer) {System.out.println (integer);}});
Program output:
12345
Skip
skip(int)
Let's ignore the first n data emitted by the observable.
Skip (int)
Filter out communities
The first 5 cells in a cell list
Observable.from(communities) .skip(5) .subscribe(new Action1<Community>() { @Override public void call(Community community) { System.out.println(community.name); } });
Skiplast
skipLast(int)
Ignores the post-n data of the observable emission.
Skiplast (int) elementat
elementAt(int)
Used to get the nth data in the sequence of events emitted by the element observable and emitted as a unique data.
ElementAt (int) debounce
debounce(long, TimeUnit)
Filters out data that is too fast to be emitted by observable, and if it has not been fired at a specified time interval, it will launch the last one. Usually we use it in conjunction with Rxbing (the Jake wharton great God uses the Android UI component of the Rxjava package) to prevent the button from repeating a click.
Debounce (Long, Timeunit)
debounce(Func1)
can be filtered according to the function in the call method of Func1, the call method in Func1 returns a temporary observable, if the original observable in the launch of a new data, The previous data is not finished with the temporary observable generated by the FUNC1 call method, and the previous data is filtered out.
Debounce (FUNC1) Distinct
distinct()
The filter rule is to allow only data that has not yet been emitted to pass, and all duplicate data items will only be emitted once.
Distinct ()
Filter out duplicates in a number:
Observable.just(2, 1, 2, 2, 3, 4, 3, 4, 5, 5) .distinct() .subscribe(new Action1<Integer>() { @Override public void call(Integer i) { System.out.print(i + " "); } });
Program output:
distinct(Func1)
The call method in the FUNC1 parameter generates a key based on the value emitted by the observable, and then compares the key to determine whether the two data is the same, and, if judged to be duplicated, distinct()
filters out duplicate data items.
Distinct (FUNC1)
Let's say we want to filter out a bunch of cells in a pile of listings where the cell name repeats:
ListNew Arraylist<> ();The first parameter in the House constructor is the cell name of the listing, and the second parameter is the property description listNew Arraylist<> (); Houses.add (New House ("Cofco/Seaview","One of the new large flat floor of Cofco Seaview!" Total Price 4500W ")); Houses.add (New House ("Chuk Yuen San Tsuen","Full five unique, prime location"); Houses.add (New House ("Chuk Yuen San Tsuen","The first floor comes with a small garden"); Houses.add (New House ("Cofco/Seaview","Adjacent to Tomson"); Houses.add (New House ("Cofco/Seaview","A premier residence for your President-like experience"); Houses.add (New House ("Chuk Yuen San Tsuen","Top floor type, two rooms and one Hall"); Houses.add (New House ("Cofco/Seaview One", "north-South permeability, deluxe five-bedroom"); Observable.from (houses). Distinct (new func1@Override public String call (House house ) { return house.communityname;}}). Subscribe (new action1@Override public void call (House house ) { System.out.println ("Cell:" + house.communityname + "; Description of the listing: "+ House.desc);}});
Program output:
小区:中粮·海景壹号; 房源描述:中粮海景壹号新出大平层!总价4500W起小区:竹园新村; 房源描述:满五唯一,黄金地段
Distinctuntilchanged
distinctUntilChanged()
and distinct()
similar, except that it determines whether the current data item of the observable launch is the same as the previous data item.
Distinctuntilchanged ()
It is also an example of filtering numbers above:
Observable.just(2, 1, 2, 2, 3, 4, 3, 4, 5, 5) .distinctUntilChanged() .subscribe(new Action1<Integer>() { @Override public void call(Integer i) { System.out.print(i + " "); } });
Program output:
distinctUntilChanged(Func1)
and distinct(Func1)
, according to the call method in Func1, a key is generated to determine whether the two adjacent data items are identical.
Distinctuntilchanged (FUNC1)
Let's take the example of the previous filter listings:
Observable.from(houses) .distinctUntilChanged(new Func1<House, String>() { @Override public String call(House house) { return house.communityName; } }).subscribe(new Action1<House>() { @Override public void call(House house) { System.out.println("小区:" + house.communityName + "; 房源描述:" + house.desc); }});
Program output:
小区:中粮·海景壹号; 房源描述:中粮海景壹号新出大平层!总价4500W起小区:竹园新村; 房源描述:满五唯一,黄金地段小区:中粮·海景壹号; 房源描述:毗邻汤臣一品小区:竹园新村; 房源描述:顶层户型,两室一厅小区:中粮·海景壹号; 房源描述:南北通透,豪华五房
First
first()
As the name implies, it is observable only sends the first data item in the observation sequence.
First ()
Get houses
The first set of listings in the listings list:
Observable.from(houses) .first() .subscribe(new Action1<House>() { @Override public void call(House house) { System.out.println("小区:" + house.communityName + "; 房源描述:" + house.desc); } });
Program output:
小区:中粮·海景壹号; 房源描述:中粮海景壹号新出大平层!总价4500W起
first(Func1)
Only the first data item that meets the criteria is sent.
First (FUNC1)
Now we are going to get houses
the first set of listings in the residential area named Chuk Yuen San Tsuen .
Observable.from(houses) .first(new Func1<House, Boolean>() { @Override public Boolean call(House house) { return "竹园新村".equals(house.communityName); } }) .subscribe(new Action1<House>() { @Override public void call(House house) { System.out.println("小区:" + house.communityName + "; 房源描述:" + house.desc); } });
Program output:
小区:竹园新村; 房源描述:满五唯一,黄金地段
Last
last()
Only the last data item in the observation sequence is emitted.
Last ()
Get the last set of listings in the listings list:
Observable.from(houses) .last() .subscribe(new Action1<House>() { @Override public void call(House house) { System.out.println("小区:" + house.communityName + "; 房源描述:" + house.desc); } });
Program output:
小区:中粮·海景壹号; 房源描述:南北通透,豪华五房
last(Func1)
Only the last data item that matches the condition in the observation sequence is emitted.
Last (FUNC1)
Get a list houses
of the last listings in the residential area named Chuk Yuen San Tsuen :
Observable.from(houses) .last(new Func1<House, Boolean>() { @Override public Boolean call(House house) { return "竹园新村".equals(house.communityName); } }) .subscribe(new Action1<House>() { @Override public void call(House house) { System.out.println("小区:" + house.communityName + "; 房源描述:" + house.desc); } });
Program output:
小区:竹园新村; 房源描述:顶层户型,两室一厅
This chapter we first talk about this, more filter class operator introduction You can go to the official documents and source code; In the next chapter we will continue to introduce the combined class operators.
Links: Https://www.jianshu.com/p/3a188b995daa
Rxjava Series 4 (filter operator)