Theodor
Theodor

Reputation: 5666

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));

Upvotes: 6

Views: 316

Answers (1)

Egor Neliuba
Egor Neliuba

Reputation: 15054

One way of doing it:

Observable<BookMoviePair> pairs =
        books.flatMap(book -> movies
                .first(movie -> movie.getId() == book.getId())
                .map(movie -> new BookMoviePair(book, movie)));

Upvotes: 4

Related Questions