@Test public void test02() { Map<String,Integer> map = new HashMap<>(); map.put("a", 21); map.put("b", 22); map.put("c", 23); Set<Entry<String,Integer>> entrySet = map.entrySet(); for (Entry<String, Integer> entry : entrySet) { System.out.println(entry.getKey() + "---->" + entry.getValue()); } }
如上代碼,新建一個Map,然后放入K和V,最后遍歷,使用JUnit5測試該方法,控制台輸出效果如下: