Java Lambda表达式遍历List<Map>集合获取特定属性值
要遍历一个List
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
List<Map<String, String>> list = new ArrayList<>();
Map<String, String> map1 = new HashMap<>();
map1.put("name", "John");
map1.put("age", "25");
list.add(map1);
Map<String, String> map2 = new HashMap<>();
map2.put("name", "Jane");
map2.put("age", "30");
list.add(map2);
// 使用Lambda表达式和Stream API遍历List<Map>集合并取出"name"属性值
List<String> names = list.stream()
.map(map -> map.get("name"))
.collect(Collectors.toList());
System.out.println(names);
}
}
运行以上代码会输出:[John, Jane],表示成功取出了List
原文地址: https://www.cveoy.top/t/topic/p94q 著作权归作者所有。请勿转载和采集!