Rxjava operator--Conditions and Boolean operators (Conditional and Boolean Operators)

Source: Internet
Author: User
Tags emit terminates throwable

Rxjava Series Tutorials:

1. Rxjava Usage Introduction "Video tutorial"
2. Rxjava operator
? Creating observables (observable creation operator) "Video tutorial"
? Transforming Observables (observable's conversion operator) "Video tutorial"
? Filtering Observables (observable filter operator) "Video tutorial"
? Combining observables (observable's combined operator) "Video tutorial"
? Error handling Operators (observable operator) "Video tutorial"
? Observable Utility Operators (auxiliary operator for Observable) "Video tutorial"
? Conditional and Boolean Operators (conditions and Boolean operators for observable) "Video tutorial"
? Mathematical and Aggregate Operators (observable mathematical operations and aggregation operators) "Video tutorial"
? Others such as Observable.tolist (), Observable.connect (), observable.publish (), etc.; "Video Tutorial"
3. RxJava Observer's relationship with Subcriber "video tutorial"
4. Rxjava Line Program Control (Scheduler) "Video tutorial"
5. RxJava concurrent data stream launch too fast how to do (back pressure (backpressure)) "Video tutorial"

All

The all operator judges all data emitted by the source observable according to a function, and the result of the final return is the result of this judgment. This function uses the emitted data as parameters, internally judging whether all of the data satisfies our defined criteria, and returns true if all are satisfied, otherwise false.

Example code:

Observable. from(studentlist). All (NewFunc1<student, boolean> () {@Override PublicBooleanPager(Student Student) {returnStudent.age > -; }}). Subscribe (NewAction1<boolean> () {@Override Public void Pager(Booleanvalue) {System. out. println ("onsuccess value ="+value); }        }); List<student> studentlist =NewArraylist<student> () {{Add (NewStudent ("Stay", -)); AddNewStudent ("Google little brother", at)); AddNewStudent ("Star", -)); }    };

Output:

valuefalse
Amb

When two or more (up to 9) observable are passed to the AMB, it only emits all the data of the observable in which the data is first emitted or notified (onerror or oncompleted), while all other observable's launchers are discarded.

There is a similar object method to Ambwith. Observable.amb (O1,O2) and O1.ambwith (O2) are equivalent.

Example code:

Observable. Amb(Observable. Just(1,2,3,4,5). Delay(2, Timeunit. SECONDS), Observable. Just( -,101)). Subscribe(New action1<integer> () {@Override public voidPager(Integer value) {System. out. println("Call value ="+ value);}                });

Output:

call value = 100call value = 101
Contains, IsEmpty, exists

The contains operator is used to determine whether the data emitted by the source observable contains a certain data, and if the inclusion returns TRUE, returns False if the source observable has ended and the data has not been emitted.

IsEmpty: An associated operator IsEmpty is used to determine whether the original observable has not emitted any data.

There is also a exists operator in Exists:rxjava that tests the data emitted by the original observable through a predicate function, and returns a observable that emits true if any one satisfies the condition. Otherwise, a observable that emits false is returned.

Example code:

//contains: Determines whether a observable emits a specific valueObservable.just (4,5,6). Contains (4). Subscribe (NewAction1<boolean> () {@Override             Public void Pager(Boolean Aboolean) {System.out.println ("contains (4):"+aboolean); }        });//isempty: Determine if the original observable has not transmitted any dataObservable.just (4,5,6). IsEmpty (). Subscribe (NewAction1<boolean> () {@Override             Public void Pager(Boolean Aboolean) {System.out.println ("IsEmpty ():"+aboolean)); }        });The //exists operator, which tests the data emitted by the original observable through a predicate function,returns a observable that emits true if any one of the conditions satisfies//Otherwise returns a observable that emits false. Observable.just (4,5,6). Exists (NewFunc1<integer, boolean> () {@Override             PublicBooleanPager(Integer integer) {returninteger<5; }}). Subscribe (NewAction1<boolean> () {@Override             Public void Pager(Boolean Aboolean) {System.out.println ("exists ():"+aboolean); }        });

Output:

contains(4):true isEmpty():false exists():true
DefaultIfEmpty

?? DefaultIfEmpty simply and accurately emits the original observable value if the original observable does not emit any data normally terminated (in the form of oncompleted), The observable returned by DefaultIfEmpty launches a default value that you provide.

Example code:

Observable.empty()        .defaultIfEmpty(10)        .subscribe(new Action1<Integer>() {            @Override            publicvoidcallvalue) {                System.out.println("defaultIfEmpty:"value);                          }        });

Output:

defaultIfEmpty:10
SequenceEqual

Determines whether two observables emit the same data sequence. Pass two observable to the sequenceequal operator, it will compare two observable of the emitter, if two sequences are the same (same data, same order, same terminating state), it will emit true, otherwise emit false.

Example code:

Observable. SequenceEqual(//First observable delay1Seconds TX Data Observable. Just(4,5,6). Delay(1, Timeunit. SECONDS), Observable. Just(4,5,6)). Subscribe(New action1<boolean> () {@Override public voidPager(Boolean Aboolean) {System. out. println("SequenceEqual:"+aboolean);}        });        . Subscribe(Aboolean, Log. V(TAG,"SequenceEqual:"+aboolean));

Output:

sequenceEqual:true
Skipuntil

Skipuntil subscribes to the original observable, but ignores its emitter until the second observable launches a data at the moment, it starts firing the original observable.

Example code:

Observable.interval (1, Timeunit.seconds)//starting from 0. Take (6). Skipuntil (Observable.just (Ten). Delay (3, timeunit.seconds)). Subscribe (NewSubscriber<long> () {@Override Public void oncompleted() {} @Override Public void OnError(Throwable e) {} @Override Public void OnNext(Longvalue) {System. out. println ("OnNext value ="+value); }        });

Output:

value3value4value5
SkipWhile

SkipWhile subscribes to the original observable, but ignores its emitter until the moment you specify a condition that becomes false, it starts firing the original observable.

Example code:

Observable.interval (1, Timeunit.seconds)//starting from 0. Take (6). SkipWhile (NewFunc1<long, boolean> () {@Override             PublicBooleanPager(Long along) {returnalong<3;//Discard the data emitted by the original observable until the transmitted data is >=3 to continue firing}}). Subscribe (NewSubscriber<long> () {@Override             Public void oncompleted() {            }@Override             Public void OnError(Throwable e) {            }@Override             Public void OnNext(Long value) {System.out.println ("SkipWhile value ="+ value); }        });

Output:

value3value4value5
Takeuntil

?? Takeuntil, in contrast to the skipuntil operator, subscribes to and starts firing the original observable, which also monitors the second observable you provide. If the second observable launches a data or launches a termination notice (onerror notification or a oncompleted notification), the observable returned by Takeuntil stops transmitting the original observable and terminates.

Example code:

Observable.interval ( -, Timeunit.milliseconds)//starting from 0. Takeuntil (Observable.just ( -,101,102). Delay (3, timeunit.seconds)). Subscribe (NewSubscriber<long> () {@Override Public void oncompleted() {} @Override Public void OnError(Throwable e) {} @Override Public void OnNext(Longvalue) {System. out. println ("Takeuntil value ="+value); }        });

Output:

value0value1value2value3value4
TakeWhile

TakeWhile, in contrast to the skipwhile operator, launches the original observable until one of the conditions you specify does not hold, it stops firing the original observable, and terminates its own observable.

Example code:

Observable.interval (1, Timeunit.seconds)//starting from 0. TakeWhile (NewFunc1<long, boolean> () {@Override             PublicBooleanPager(Long along) {returnalong<3; }}). Subscribe (NewSubscriber<long> () {@Override             Public void oncompleted() {            }@Override             Public void OnError(Throwable e) {            }@Override             Public void OnNext(Long value) {System.out.println ("TakeWhile value ="+ value); }        });

Output:

value0value1value2

Rxjava operator--Conditions and Boolean operators (Conditional and Boolean Operators)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.