【题目】说出两种方法遍历一个HashMap,获取所有的Key和Value?

解决方案 »

  1.   

    for-each,迭代器
      

  2.   

        @org.junit.jupiter.api.Test
        public void d(){
            HashMap<Integer, String> map = new HashMap<>();
            map.put(1, "1s");
            map.put(2, "2s");
            map.put(3, "3s");        for (Integer key:map.keySet()) {
                System.out.println("key=" + key + ",value=" + map.get(key));
            }
            System.out.println("--------------");
            for (Map.Entry<Integer, String> entries : map.entrySet()) {
                System.out.println("key=" + entries.getKey() + ",value=" + entries.getValue());
            }
        }