欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java 遍历MAP的几种方法示例代码

程序员文章站 2024-03-13 18:40:57
java中遍历map的几种方法 map map=new hashmap(...

java中遍历map的几种方法

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循环

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

第二种用迭代

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()迭代

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()迭代

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

 

 以上就是对java 遍历map的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!