This is my list:
[
{name: 'moe', age: 40},
{name: 'larry', age: 50},
{name: 'curly', age: 60}
];
I want to wrest the values nameand create another one Listas follows:
["moe", "larry", "curly"]
I wrote 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 when using stream. I tried this code that does not work.
List<String> newList = list.stream().map(x -> x.get("name")).collect(Collectors.toList());
source
share