zoukankan      html  css  js  c++  java
  • java中Map遍历的四种方式

    java中Map遍历的四种方式



    在java中所有的map都实现了Map接口,因此所有的Map(如HashMap, TreeMap, LinkedHashMap, Hashtable等)都可以用以下的方式去遍历。

    方法一:在for循环中使用entries实现Map的遍历:

    /**
    * 最常见也是大多数情况下用的最多的,一般在键值对都需要使用
     */
    Map <String,String>map = new HashMap<String,String>();
    map.put("熊大", "棕色");
    map.put("熊二", "黄色");
    for(Map.Entry<String, String> entry : map.entrySet()){
        String mapKey = entry.getKey();
        String mapValue = entry.getValue();
        System.out.println(mapKey+":"+mapValue);
    }
    



    方法二:在for循环中遍历key或者values,一般适用于只需要map中的key或者value时使用,在性能上比使用entrySet较好;

    Map <String,String>map = new HashMap<String,String>();
    map.put("熊大", "棕色");
    map.put("熊二", "黄色");
    //key
    for(String key : map.keySet()){
        System.out.println(key);
    }
    //value
    for(String value : map.values()){
        System.out.println(value);
    }
    



    方法三:通过Iterator遍历;

    Iterator<Entry<String, String>> entries = map.entrySet().iterator();
    while(entries.hasNext()){
        Entry<String, String> entry = entries.next();
        String key = entry.getKey();
        String value = entry.getValue();
        System.out.println(key+":"+value);
    }
    



    方法四:通过键找值遍历,这种方式的效率比较低,因为本身从键取值是耗时的操作;

    for(String key : map.keySet()){
        String value = map.get(key);
        System.out.println(key+":"+value);
    }
    

    转载自:https://blog.csdn.net/weixin_45395031/article/details/109174799

  • 相关阅读:
    23.课程应用接口
    22.课程页面设计
    21.手机接口
    20.云通讯
    19.JWT
    18.权限认证
    解决github下载慢的终极方法
    vs code 配置c/c++环境
    Python 字符编码处理总结
    Python编码
  • 原文地址:https://www.cnblogs.com/k-class/p/13972052.html
Copyright © 2011-2022 走看看