码迷,mamicode.com
首页 > 其他好文 > 详细

map遍历

时间:2016-06-16 13:19:40      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

java中遍历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");  
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());   
}  
for(Map.Entry<String, String> entry:map.entrySet()){
          System.out.println(entry.getKey()+"--->"+entry.getValue());
}

第二种用迭代
Java代码

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());   
}  
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()迭代
Java代码

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);   
}  
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);
}

用entrySet()迭代
Java代码

Iterator it=map.entrySet().iterator();          
System.out.println( map.entrySet().size());   
String key;          
String value;   
while(it.hasNext()){   
        Map.Entry entry = (Map.Entry)it.next();          
        key=entry.getKey().toString();          
        value=entry.getValue().toString();          
        System.out.println(key+"===="+value);                    
}  

map遍历

标签:

原文地址:http://www.cnblogs.com/zhuzhuxuan/p/5590587.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!