VB_
VB_

Reputation: 45722

RxJava: wait another observable result

How to wait another observable result while transforming the current one?

I have a list of user ids (userIdsList), and I should convert them to a map. In map key is represented by userId and value is boolean, which indicates if userId containsin regUsers.

return Observable
   .<List<Long>>from(userIdsList)
   .groupBy(id -> id, id -> regUsers.contains(id)); //PROBLEM: regUsers is Observable<List<Long>>

Upvotes: 2

Views: 465

Answers (1)

m.ostroverkhov
m.ostroverkhov

Reputation: 1960

Somewhat convoluted solution.

      List<Long> ids = Arrays.asList(1L, 2L, 3L);
      Observable<List<Long>> regUsers = Observable.just(Arrays.asList(1L, 2L)).cache();

      Observable<Long> idsStream = Observable.from(ids).cache();
      Observable<Boolean> containsStream = idsStream
             .concatMap(id -> regUsers.map(regList -> regList.contains(id)));

      idsStream.zipWith(containsStream, (Func2<Long, Boolean, Pair>) Pair::new)
            .toMap(Pair::getLeft, Pair::getRight).subscribe(System.out::println);


  private static class Pair<K, V> {

    private final K left;
    private final V right;

    public Pair(K left, V right) {
        this.left = left;
        this.right = right;
    }

    public K getLeft() {
        return left;
    }

    public V getRight() {
        return right;
    }
}

Upvotes: 2

Related Questions