Skip to content

Instantly share code, notes, and snippets.

@lsurvila
Last active February 7, 2019 04:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lsurvila/bdf00664d296ea5885cc to your computer and use it in GitHub Desktop.
Save lsurvila/bdf00664d296ea5885cc to your computer and use it in GitHub Desktop.
An example how to iterate and filter through list which is inside object, retrieved from server/etc with RxJava
public Observable<SomeResponse> downloadAndFilterListInResponse() {
return mSomeRetrofitApi.downloadSomeData() // get response from server/etc
// start iterating through list in response object
.flatMap(response -> Observable.from(response.getListWeWantToFilterOut())
// NOTE: this is inner sequence, gets result from Observable.from
.filter(item -> item != null) // filters out items that are null
.toList() // all items combined into list again
.take(100) // take only first 100 items, rest will be ignored
.map(items -> { // now we need to 'map' list into response
response.setListWeWantToFilterOut(items); // overwrite list in object with filtered list
return response; // return modified response
}));
}
public class SomeResponse {
private List<String> listWeWantToFilterOut;
private int someIntegerWeWantToKeep;
private String someOtherValueWeWantToKeep;
public List<String> getListWeWantToFilterOut() {
return listWeWantToFilterOut;
}
// in this example list must has required access
public void setListWeWantToFilterOut(List<String> listWeWantToFilterOut) {
this.listWeWantToFilterOut = listWeWantToFilterOut;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment