Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava - How to take the first element of a list and return it as Observable

Let's take this observable:

Observable<List<UserProfile>> findUser =service.getUserProfiles()

How can I transform it so it returns the first element as an Observable (and not an Observable list just containing the first element). I tried first() and takeFirst() but it still returns a list.

like image 995
znat Avatar asked Jul 05 '16 20:07

znat


1 Answers

Map it!

Observable<List<UserProfile>> findUser = service.getUserProfiles();
Observable<UserProfile> firstUser = findUser
    .filter(list -> !list.isEmpty())
    .map(list -> list.get(0));
like image 84
akarnokd Avatar answered Sep 23 '22 01:09

akarnokd