public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("a", "1");
map.put("b", "2");
map.put("c", "3");
//jdk8之前 forEach and Map
//一般情况下遍历Map的一般方法.
for (String key : map.keySet()) {
System.out.println("key=" + key + "\t value = " + map.get(key));
System.out.println();
}
//Java 8中, 我们能够遍历 用forEach
//通过lambda 表达式遍历Map
map.keySet().forEach(
// 这里的key 是通过匿名变量 命名的
key -> System.out.println("key:"+key + "<---" + map.get(key))
);
}
运行结果:
key=a value = 1
key=b value = 2
key=c value = 3
key:a<---1
key:b<---2
key:c<---3
Process finished with exit code 0
转载:https://blog.csdn.net/qq_15021257/article/details/81102586