GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Conditional and Boolean Operators

Conditional and Boolean Operators

Jump to bottom
akarnokd edited this page Jun 26, 2020 · 28 revisions

This section explains operators with which you conditionally emit or transform Observables, or can do boolean evaluations of them:

Conditional Operators

Outline

  • amb
  • defaultIfEmpty
  • skipUntil
  • skipWhile
  • takeUntil
  • takeWhile

amb

Available in: image Flowable, image Observable, image Maybe, image Single, image Completable

ReactiveX documentation: http://reactivex.io/documentation/operators/amb.html

given two or more source Observables, emits all of the items from the first of these Observables to emit an item

    Observable source1 = Observable.range(1, 5);
    Observable source2 = Observable.range(6, 5);
    Observable.amb(new ArrayList(Arrays.asList(source1, source2)))
        .subscribe(next -> System.out.printf("next: %s\n", next), // onNext
            throwable -> System.out.printf("error: %s\n", throwable), //onError
            () -> System.out.println("Completed") //onComplete
        );

defaultIfEmpty

Available in: image Flowable, image Observable, image Maybe, image Single, image Completable

ReactiveX documentation: http://reactivex.io/documentation/operators/defaultifempty.html

emit items from the source Observable, or emit a default item if the source Observable completes after emitting no items

 Observable.empty().defaultIfEmpty(1).blockingSubscribe(next -> System.out.printf("next: %s\n", next), // onNext
                throwable -> System.out.printf("error: %s", throwable), //onError
                () -> System.out.println("Completed") //onComplete
        );

skipUntil

Available in: image Flowable, image Observable, image Maybe, image Single, image Completable

ReactiveX documentation: http://reactivex.io/documentation/operators/skipuntil.html

discard items emitted by a source Observable until a second Observable emits an item, then emit the remainder of the source Observable's items

Observable observable1 = Observable.range(1, 10).doOnNext(next -> Thread.sleep(1000));
        
observable1.skipUntil(Observable.timer(3, TimeUnit.SECONDS))
    .subscribe(next -> System.out.printf("next: %s\n", next), // onNext
        throwable -> System.out.printf("error: %s", throwable), //onError
        () -> System.out.println("Completed") //onComplete
    );

skipWhile

Available in: image Flowable, image Observable, image Maybe, image Single, image Completable

ReactiveX documentation: http://reactivex.io/documentation/operators/skipwhile.html

discard items emitted by an Observable until a specified condition is false, then emit the remainder

Observable.range(1, 10).skipWhile(next -> next < 5)
    .subscribe(next -> System.out.printf("next: %s\n", next), // onNext
        throwable -> System.out.printf("error: %s", throwable), //onError
        () -> System.out.println("Completed") //onComplete
    );

takeUntil

Available in: image Flowable, image Observable, image Maybe, image Single, image Completable

ReactiveX documentation: http://reactivex.io/documentation/operators/takeuntil.html

emits the items from the source Observable until a second Observable emits an item or issues a notification

Observable.range(1, 10).takeUntil(value -> value >= 5)
    .subscribe(next -> System.out.printf("next: %s\n", next), // onNext
        throwable -> System.out.printf("error: %s", throwable), //onError
        () -> System.out.println("Completed") //onComplete
    );

takeWhile

Available in: image Flowable, image Observable, image Maybe, image Single, image Completable

ReactiveX documentation: http://reactivex.io/documentation/operators/takewhile.html

emit items emitted by an Observable as long as a specified condition is true, then skip the remainder

     Observable.range(1, 10).takeWhile(value -> value <= 5)
                .subscribe(next -> System.out.printf("next: %s\n", next), // onNext
                        throwable -> System.out.printf("error: %s", throwable), //onError
                        () -> System.out.println("Completed") //onComplete
                );

Boolean Operators

Outline

  • all
  • contains
  • isEmpty
  • sequenceEqual

all

Available in: image Flowable, image Observable, image Maybe, image Single, image Completable

ReactiveX documentation: http://reactivex.io/documentation/operators/all.html

determine whether all items emitted by an Observable meet some criteria

Flowable.range(0,10).doOnNext(next -> System.out.println(next)).all(integer -> integer<10).
    blockingSubscribe(success->System.out.println("Success: "+success));

contains

Available in: image Flowable, image Observable, image Maybe, image Single, image Completable

ReactiveX documentation: http://reactivex.io/documentation/operators/contains.html

determine whether an Observable emits a particular item or not

Flowable.range(1,10).doOnNext(next->System.out.println(next))
    .contains(4).blockingSubscribe(contains->System.out.println("contains: "+contains));

isEmpty

Available in: image Flowable, image Observable, image Maybe, image Single, image Completable

ReactiveX documentation: http://reactivex.io/documentation/operators/contains.html

determine whether the source Publisher is empty

Flowable.empty().isEmpty().subscribe(isEmpty -> System.out.printf("isEmpty: %s", isEmpty));

sequenceEqual

Available in: image Flowable, image Observable, image Maybe, image Single, image Completable

ReactiveX documentation: http://reactivex.io/documentation/operators/sequenceequal.html

test the equality of the sequences emitted by two Observables

Flowable<Integer> flowable1 = Flowable.range(1,3).doOnNext(next-> System.out.print("flowable1: "+next + " "));

Flowable<Integer> flowable2 = Flowable.range(1,3).doOnNext(next-> System.out.println("flowable2: "+next));

Flowable.sequenceEqual(Flowable.fromPublisher(flowable1),Flowable.fromPublisher(flowable2))
    .blockingSubscribe(sequenceEqual->System.out.println("sequenceEqual: "+sequenceEqual));

Copyright (c) 2016-present, RxJava Contributors.
Twitter @RxJava | Gitter @RxJava

  • Introduction
  • Getting Started
  • How to Use RxJava
  • Reactive Streams
  • The reactive types of RxJava
  • Schedulers
  • Subjects
  • Error Handling
  • Operators (Alphabetical List)
    • Async
    • Blocking
    • Combining
    • Conditional & Boolean
    • Connectable
    • Creation
    • Error management
    • Filtering
    • Mathematical and Aggregate
    • Parallel flows
    • String
    • Transformation
    • Utility
    • Notable 3rd party Operators (Alphabetical List)
    • Operator matrix
  • Plugins
  • How to Contribute
  • Writing operators
  • Backpressure
    • another explanation
  • JavaDoc: 1.x, 2.x, 3.x
  • Coming from RxJava 1
  • Coming from RxJava 2
  • Additional Reading

Clone this wiki locally

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.