zoukankan      html  css  js  c++  java
  • 如何遍历Map对象

    方法一 通过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);
            }
        }

  • 相关阅读:
    云计算的三种服务模式:IaaS, PaaS, SaaS
    Docker 容器备份例子
    软件版本号
    git 命令小总结
    【Oracle】ORA-12560: TNS: 协议适配器错误
    【VMware】The VMX process exited permaturely
    Linux(CentOS)安装SQL Server
    Linux源码编译安装httpd
    Linux安装MySQL
    Linux安装Tomcat
  • 原文地址:https://www.cnblogs.com/TangGe520/p/8916675.html
Copyright © 2011-2022 走看看