Last active
February 22, 2020 14:07
-
-
Save bowmanb/a93185389210e3bd2eb8 to your computer and use it in GitHub Desktop.
RxJava collect() example. Converting a list of phrases into a list of their IDs only.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void onNext(ArrayList<Phrase> phrases) { | |
ArrayList<Integer> phraseIDs = new ArrayList<>(); | |
Observable | |
.from(phrases) | |
.collect(phraseIDs, new Action2<ArrayList<Integer>, Phrase>() { | |
@Override | |
public void call(ArrayList<Integer> integers, Phrase phrase) { | |
integers.add(phrase.getId()); | |
} | |
}) | |
.subscribe(); | |
} |
List<Integer> phraseIDs = new ArrayList<>();
Observable.fromArray(phrases)
.flatMapIterable(x -> x)
.collect(() -> phraseIDs, (l, p) -> l.add(p.getId()))
.subscribe();
Thanks, this was helpful.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you show an example of how to do the same in RxJava2?