2

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>>
VB_
  • 45,112
  • 42
  • 145
  • 293

1 Answers1

2

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;
    }
}
m.ostroverkhov
  • 1,910
  • 1
  • 15
  • 17