JAVA8-Lambda-forEach遍历List/Map
一、遍历List
代码示例
public static void main(String[] args) {
List<String> list = Arrays.asList("北", "上", "广", "深");
list.forEach(System.out::println);
}
输出结果:
二、遍历Map
代码示例
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("姓名", "张三");
map.put("年龄", "12");
map.put("地址", "月球");
map.forEach((k, v) -> System.out.println(k + ":" + v));
}