ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Exploring RxJava in Android — Conditional and Boolean Operators

This article is part of RxJava Introduction series. You can checkout the entire series here:

Conditional and Boolean Operators

All

This operator determines whether all items emitted by an Observable meet some criteria.

Sample Implementation: The below code demonstrates the use of all() operator.

Output

onNext: false //false since the input contains 0

Amb

When you pass a number of source Observables to amb(), it will pass through the emissions and notifications of exactly one of these Observables: the first one that sends a notification to Amb, either by emitting an item or sending an onError or onCompleted notification. Amb will ignore and discard the emissions and notifications of all of the other source Observables.

Sample Implementation: The below code demonstrates the use of amb() operator. In the below example, the observable2 gets executed because it is the first to emit its first item. observable1 is ignored.

Output

onNext: 100
onNext: 200
onNext: 300
onNext: 400
onNext: 500

Contains

This operator determines whether an Observable emits a particular item or not.

Sample Implementation: The below code demonstrates the use of contains() operator.

Output

Does list contain value 10: false

DefaultIfEmpty

This operator emits items from the source Observable, or a default item if the source Observable emits nothing.

Sample Implementation: The below code demonstrates the use of defaultIfEmpty() operator. The below code generates a random number and if the number is an even number, it is emitted, otherwise the Observable completes its emission and a default value we have specified (-10 in this case), is emitted.

Output

onNext: -10

SequenceEqual

This operator determines whether two Observables emit the same sequence of items.

Sample Implementation: The below code demonstrates the use of sequenceEqual() operator.

Output

Are the two sequences equal: true

SkipUntil

This operator discards items emitted by an Observable until a second Observable emits an item.

Sample Implementation: The below code demonstrates the use of skipUntil() operator. In the below example, after emitting 3, 4, 5, the second Observable starts emitting items after three seconds.

Output

onNext: 3
onNext: 4
onNext: 5

SkipWhile

This operator will discard items emitted by an Observable until a specified condition becomes false.

Sample Implementation: The below code demonstrates the use of skipWhile() operator. In the below example, we have specified a list of integers from 0 to 6. All items are ignored until the given condition fails.

Output

onNext: 2
onNext: 3
onNext: 4
onNext: 5
onNext: 6

TakeUntil

This operator discards items emitted by an Observable until after a second Observable emits an item or terminates.

Sample Implementation: The below code demonstrates the use of takeUntil() operator. It is the exact opposite of skipUntil() .

Output

onNext: 0
onNext: 1

TakeWhile

This operator will discard items emitted by an Observable after a specified condition becomes false.

Sample Implementation: The below code demonstrates the use of takeWhile() operator. In the below example, we have specified a list of integers from 0 to 6. All items are emitted until the given conditions fails.

Output

onNext: 0
onNext: 1

That’s it guys! This is part six of the series on RxJava. I hope you enjoyed this article and found it useful, if so please hit the Clap button. Let me know your thoughts in the comments section.

Happy coding!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response