标签:pac   --   stat   ext   操作   codeblock   通过   new   iterator   
一、遍历Map的4种方法
在java中所有的map都实现了Map接口,因此所有的Map(如HashMap, TreeMap, LinkedHashMap, Hashtable等)都可以用以下的方式去遍历。
public static void main(String[] args) {
    Map <String,String>map = new HashMap<>();
    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);
    }
}
 
public static void main(String[] args) {
    Map <String,String>map = new HashMap<>();
    map.put("熊大", "棕色");
    map.put("熊二", "黄色");
 
    for(String key : map.keySet()){
        System.out.println(key); //熊大
    }
 
    for(String value : map.values()){
        System.out.println(value); //棕色
    }
}
 
public static void main(String[] args) {
    Map <String,String>map = new HashMap<>();
    map.put("熊大", "棕色");
    map.put("熊二", "黄色");
 
    Iterator<Map.Entry<String, String>> entries = map.entrySet().iterator();
    while(entries.hasNext()){
        Map.Entry<String, String> entry = entries.next();
        System.out.println(entry.getKey() + ":" + entry.getValue()); //熊大:棕色
    }
}
 
public static void main(String[] args) {
    Map <String,String>map = new HashMap<>();
    map.put("熊大", "棕色");
    map.put("熊二", "黄色");
 
    for(String key : map.keySet()){
        System.out.println(key + "-->" +map.get(key)); //熊大-->棕色
    }
}
 
 
遍历Map的4种方法(来自网络)
标签:pac   --   stat   ext   操作   codeblock   通过   new   iterator   
原文地址:https://www.cnblogs.com/duomen/p/13245068.html