zoukankan      html  css  js  c++  java
  • Java——Iterate through a HashMap

    遍历Map

    import java.util.*;
    
    
    public class IterateHashMap {
    
        public static void main(String[] args) {
            Map<String,Object> map=new  HashMap<String,Object>();
            // If you're only interested in the keys, you can iterate through the keySet() of the map:
            for (String key : map.keySet())
            {
                // ...
            }
            //If you only need the values, use values():
            for (Object value : map.values()) 
            {
                // ...
            }
            //Finally, if you want both the key and value, use entrySet():
            for (Map.Entry<String, Object> entry : map.entrySet()) 
            {
                String key = entry.getKey();
                Object value = entry.getValue();
                // ...
            }
            //
            Iterator it = map.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry pair = (Map.Entry)it.next();
                System.out.println(pair.getKey() + " = " + pair.getValue());
                it.remove(); // avoids a ConcurrentModificationException
            }
    
        }
    }

    来源:http://stackoverflow.com/questions/1066589/iterate-through-a-hashmap

  • 相关阅读:
    [POI2000]病毒
    枪战(maf)
    禅与园林艺术(garden)
    The Cave
    集合选数
    BZOJ3990 排序(sort)
    区间(interval)
    太空飞船(spaceship)
    数表( table )
    Printed Circuit Board (board)
  • 原文地址:https://www.cnblogs.com/zhangzhi19861216/p/5357745.html
Copyright © 2011-2022 走看看