This is my List:
[
{name: 'moe', age: 40},
{name: 'larry', age: 50},
{name: 'curly', age: 60}
];
I want to pluck name
values and create another List
like this:
["moe", "larry", "curly"]
I've written this code and it works:
List<String> newList = new ArrayList<>();
for(Map<String, Object> entry : list) {
newList.add((String) entry.get("name"));
}
But how to do it in using stream
. I've tried this code which doesn't work.
List<String> newList = list.stream().map(x -> x.get("name")).collect(Collectors.toList());