HashMap 的 7 种遍历方式与性能分析!

参考来自于:
HashMap 的 7 种遍历方式与性能分析!

方法之1: 使用 forEach

public class HashMapTest {
    public static void main(String[] args) {
        // 创建并赋值 HashMap
        Map<Integer, String> map = new HashMap();
        map.put(1, "Java");
        map.put(2, "JDK");
        map.put(3, "Spring Framework");
        map.put(4, "MyBatis framework");
        map.put(5, "Java中文社群");
        // 遍历
        map.forEach((key, value) -> {
            System.out.println(key);
            System.out.println(value);
        });
    }
}
posted @ 2022-11-07 10:20  aaacarrot  阅读(34)  评论(0编辑  收藏  举报