这是我的清单:流过地图的列表,并收集特定键
[
{name: 'moe', age: 40},
{name: 'larry', age: 50},
{name: 'curly', age: 60}
];
我想掐name
值,然后再创建List
这样的:
["moe", "larry", "curly"]
我写这个代码和它的工作原理:
List<String> newList = new ArrayList<>();
for(Map<String, Object> entry : list) {
newList.add((String) entry.get("name"));
}
但如何在使用stream
。我试过这个代码不起作用。
List<String> newList = list.stream().map(x -> x.get("name")).collect(Collectors.toList());
仅供参考,这些都不是JSON。我刚刚用它们来表示我的'List'对象。 – Snow