Last active
October 31, 2017 11:52
-
-
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.
This file contains hidden or 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
| 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