Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava join observable streams by matching attribute value

Lets say I have two observable streams

Observable<Book> books;
Observable<Movie> movies;

How can I join these on when they have an attribute that matches? Something like the psudo code below:

Observable<BookMoviePair> pairs = books.join(movies)
    .where((book, movie) -> book.getId() == movie.getId()))
    .return((book, movie) -> new BookMoviePair(book, movie));
like image 364
Theodor Avatar asked Mar 13 '16 10:03

Theodor


1 Answers

One way of doing it:

Observable<BookMoviePair> pairs =
        books.flatMap(book -> movies
                .first(movie -> movie.getId() == book.getId())
                .map(movie -> new BookMoviePair(book, movie)));
like image 97
Egor Neliuba Avatar answered Oct 06 '22 00:10

Egor Neliuba