前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Java遍历map集合的4中方式

Java遍历map集合的4中方式

作者头像
全栈程序员站长
发布2022-07-23 15:44:57
发布2022-07-23 15:44:57
49200
代码可运行
举报
运行总次数:0
代码可运行

大家好,又见面了,我是你们的朋友全栈君。

方法一 通过Map.entrySet遍历key和value,在for-each循环中使用entries来遍历.推荐,尤其是容量大时

这是最常见的并且在大多数情况下也是最可取的遍历方式。在键值都需要时使用。

代码语言:javascript
代码运行次数:0
运行
复制
Map<Integer, Integer> map = new HashMap<Integer, Integer>();  
  
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {  
  
    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());  
  
}  

方法二、通过Map.keySet遍历key,通过键找值value遍历(效率低),普遍使用,二次取值

代码语言:javascript
代码运行次数:0
运行
复制
Map<Integer, Integer> map = new HashMap<Integer, Integer>();  
  
for (Integer key : map.keySet()) {  
  
    Integer value = map.get(key);  
  
    System.out.println("Key = " + key + ", Value = " + value);  
  
}

方法三 如果只需要map中的键或者值,你可以通过Map.keySet或Map.values来实现遍历,而不是用entrySet。在for-each循环中遍历keys或values。

代码语言:javascript
代码运行次数:0
运行
复制
Map<Integer, Integer> map = new HashMap<Integer, Integer>();  
  
//遍历map中的键  
  
for (Integer key : map.keySet()) {  
  
    System.out.println("Key = " + key);  
  
}  

//遍历map中的值  
  
for (Integer value : map.values()) {  
  
    System.out.println("Value = " + value);  
  
}  

方法四:通过Map.entrySet使用iterator遍历key和value

代码语言:javascript
代码运行次数:0
运行
复制
Map<Integer, Integer> map = new HashMap<Integer, Integer>();  
  
Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();  
  
while (entries.hasNext()) {  
  
    Map.Entry<Integer, Integer> entry = entries.next();  
  
    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());  
  
}  

全部代码:

代码语言:javascript
代码运行次数:0
运行
复制
public static void main(String[] args) {
        init();
        traversal();
    }

    // HashMap按照哈希算法来存取键对象,有很好的存取性能
    private static HashMap<String, String> hMap = new HashMap<>();

    // TreeMap实现了SortedMap接口,能对键对象进行排序。支持自然排序和客户化排序两种方式。
    private static TreeMap<String, String> tMap = new TreeMap<>();

    // map的赋值
    public static void init() {
        hMap.put("1", "value1");
        hMap.put("2", "value2");
        hMap.put("3", "value3");
    }

    // map的遍历
    public static void traversal() {
        // 第一种:普遍使用,二次取值
        System.out.println("通过Map.keySet遍历key和value:");
        for (String key : hMap.keySet()) {
            System.out.println("key= " + key + " and value= " + hMap.get(key));
        }

        // 第二种
        System.out.println("通过Map.entrySet使用iterator遍历key和value:");
        Iterator<Map.Entry<String, String>> it = hMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> entry = it.next();
            System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
        }

        // 第三种:推荐,尤其是容量大时
        System.out.println("通过Map.entrySet遍历key和value");
        for (Map.Entry<String, String> entry : hMap.entrySet()) {
            System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
        }

        // 第四种
        System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
        for (String v : hMap.values()) {
            System.out.println("value= " + v);
        }
    }

运行结果:

通过Map.keySet遍历key和value: key= 1 and value= value1 key= 2 and value= value2 key= 3 and value= value3 通过Map.entrySet使用iterator遍历key和value: key= 1 and value= value1 key= 2 and value= value2 key= 3 and value= value3 通过Map.entrySet遍历key和value key= 1 and value= value1 key= 2 and value= value2 key= 3 and value= value3 通过Map.values()遍历所有的value,但不能遍历key value= value1 value= value2 value= value3

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/126763.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月8,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档