Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava filter 'else'

I want to split my observable like if/else statement

Something like:

A[] array = new A[10];
for (int i = 0; i < array.length; i++) {
    array[i] = new A(getRandomString(), getRandomInt());
}

Observable.from(array)
    .filter(a -> condition(a))
        .// <--- do stuff  if condition returns true
        .// <- back to parent
    .filter(a -> complexCondition(a)) // filter all elements(!)
        .// <--- do stuff if complex condition returns true
        .// <- back to iterate all elements

Is it even possible?

like image 446
Lau Avatar asked Jun 07 '16 13:06

Lau


People also ask

What is RX observable?

There are two key types to understand when working with Rx: Observable represents any object that can get data from a data source and whose state may be of interest in a way that other objects may register an interest. An observer is any object that wishes to be notified when the state of another object changes.

What are operators in RxJava?

Code your next android app using RxJava Use of Rx operators. With RxOperator are basically a function that defines the observable, how and when it should emit the data stream. There are hundreds of operators available in RxJava. You can read an alphabetical list of all the operators available from here.

What is single RxJava?

Single is an Observable that always emit only one value or throws an error. A typical use case of Single observable would be when we make a network call in Android and receive a response. Sample Implementation: The below code always emits a Single user object. We use a Single Observable and a Single Observer.

Why use RxJava?

RxJava is a JVM library that uses observable sequences to perform asynchronous and event-based programming. Its primary building blocks are triple O's, which stand for Operator, Observer, and Observables. And we use them to complete asynchronous tasks in our project. It greatly simplifies multithreading in our project.


1 Answers

In Observable does not work in that way, you have to think that the observable is just like a stream, where you can end up with some items, u others. The most close if/else that you will find in observables will be use GroupsBy.

Here you have some practicle example that I did explaining how works

https://github.com/politrons/reactive/blob/master/src/test/java/rx/observables/transforming/ObservableGroupBy.java

like image 92
paul Avatar answered Nov 16 '22 09:11

paul