Map遍历两种方式

Java代码
    Map<String,String> map=new HashMap<String,String>();   
    map.put("username", "qq");   
    map.put("passWord", "123");   
    map.put("userID", "1");   
    map.put("email", "qq@qq.com");  

第一种用for循环
Java代码

    for(Map.Entry<String, String> entry:map.entrySet()){   
         System.out.println(entry.getKey()+"--->"+entry.getValue());   
    }  

第二种用迭代

用entrySet();
    Set set = map.entrySet();        
    Iterator i = set.iterator();        
    while(i.hasNext()){     
         Map.Entry<String, String> entry1=(Map.Entry<String, String>)i.next();   
         System.out.println(entry1.getKey()+"=="+entry1.getValue());   
    }  

用keySet()迭代
    Iterator it=map.keySet().iterator();   
    while(it.hasNext()){   
         String key;   
         String value;   
         key=it.next().toString();   
         value=map.get(key);   
         System.out.println(key+"--"+value);   
    }  
 
posted @ 2016-04-15 17:53  goumingming  阅读(138)  评论(0编辑  收藏  举报