Java遍历map集合的4中方式

方法一通过Map.entrySet遍历key和value,在for-each循环中使用entries来遍历.推荐,尤其是容量大时这是最常见的并且在大多数情况下也是最可取的遍历方式。在键值都需要时使用。Map<Integer,Integer>map=newHashMap<Integer,Integer>();for(Map.Entry&l…

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

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

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

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遍历(效率低),普遍使用,二次取值

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。

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

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());  
  
}  

全部代码:


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.net/126763.html原文链接:https://javaforall.net

(0)
上一篇 2022年4月8日 下午12:20
下一篇 2022年4月8日 下午12:20


相关推荐

  • pycharm的字体设置_pycharm怎么设置字体

    pycharm的字体设置_pycharm怎么设置字体设置的路径是File->settings->Editor->FileandCodeTemplates->PythonScript再来看下效果:设置OK

    2022年8月29日
    6
  • webservices协议_webservice框架有哪些

    webservices协议_webservice框架有哪些Webservice三种规范•1、JAX-WSJAX-WS是JAX-RPC的演进版本,但JAX-WS并不完全向后兼容JAX-RPC,二者最大的区别就是RPC/encoded样式的WSDL,JAX-WS已经不提供这种支持。•2、JAXM&SAAJJAXM(JAVAAPIForXMLMessage)主

    2025年12月2日
    9
  • 完整记录一则Oracle 11.2.0.4单实例打PSU补丁的过程

    完整记录一则Oracle 11.2.0.4单实例打PSU补丁的过程本文记录了打 PSU 的全过程 意在体会数据库打 PSU 补丁的整个过程 1 OPatch 替换为最新版本 2 数据库软件应用补丁程序 3 数据库应用补丁 4 验证 PSU 补丁是否应用成功 1 OPatch 替换为最新版本 oracle DBusr2 iduid 500 oracle gid 500 oinstall 组 500 oinstall 501 dba 环境

    2026年3月18日
    1
  • JSP注释实战

    JSP注释实战一代码 lt pagecontentT text html charset GBK language java errorPage gt DOCTYPE tmlPUBLIC W3C DTDXHTML1 0Transitiona EN http www w3 org TR xhtml1 DTD xhtml1

    2026年3月17日
    1
  • ucGUI学习小结

    ucGUI学习小结简要总结了自己学习 ucGUI 并应用到工程中的整个过程 并提供整理好的相关资料下载

    2026年3月18日
    2
  • idea2021.1.4激活码【最新永久激活】

    (idea2021.1.4激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlS3…

    2022年3月26日
    256

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号