Skip to content

Instantly share code, notes, and snippets.

@dinolupo
Last active October 31, 2017 11:52
Show Gist options
  • Select an option

  • Save dinolupo/f91be5f8741122bea975da3915c202b6 to your computer and use it in GitHub Desktop.

Select an option

Save dinolupo/f91be5f8741122bea975da3915c202b6 to your computer and use it in GitHub Desktop.
JSON Collectors are useful when working with streams and filtering a JsonArray with a results into another JsonArray. In the example below you are filtering for a female gender and creating a JsonArray with all the selected names.
JsonArray contacts = ...;
JsonArray femaleNames =
contacts.getValuesAs(JsonObject.class).stream()
.filter(x->"F".equals(x.getString("gender")))
.map(x->(x.getString("name"))
.collect(JsonCollectors.toJsonArray());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment