I am parsing the JSON response from server in Java class. We have utility function to covert JSON to List<Map<String, Object>>.
Now I want to collect specific key's value from all objects. For example the JSON I receive is person's data like below
[
{
name:'abc',
city: 'Hyd'
},
{
name:'def',
city:'NYC'
}
]
I wanted to try Java8 stream API to get this job done, so here is how i wrote
List<Map<String,Object>> answer = parseJSONReponse(response);
List<Object> collect = answer.stream().map(
m -> m.entrySet().stream()
.filter(e-> e.getKey().equals("name"))
.map(e -> e.getValue())
.collect(Collectors.toList())
).flatMap(l->l.stream()).collect(Collectors.toList());
Please let me know if there is better of writing this stream API.