如何通过map.entry遍历Map

   2024-10-07 7050
核心提示:可以通过以下方式遍历Map的entry:MapString, Integer map = new HashMap();map.put(A, 1);map.put(B, 2);map.put(C, 3);for (Ma

可以通过以下方式遍历Map的entry:

Map<String, Integer> map = new HashMap<>();map.put("A", 1);map.put("B", 2);map.put("C", 3);for (Map.Entry<String, Integer> entry : map.entrySet()) {    String key = entry.getKey();    Integer value = entry.getValue();    System.out.println("Key: " + key + ", Value: " + value);}

在上面的例子中,我们首先通过entrySet()方法获取Map的entry集合,然后使用增强型for循环遍历每个entry,最后可以通过getKey()getValue()方法获取每个entry的键和值。

 
举报打赏
 
更多>同类维修大全
推荐图文
推荐维修大全
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号