java中遍历MAP的几种方法Java代码
Map map=new
HashMap();
map.put("username",
"qq");
map.put("passWord",
"123");
map.put("userID",
"1");
map.put("email",
"qq@qq.com");
Map map=new
HashMap();
map.put("username",
"qq");
map.put("passWord",
"123");
map.put("userID",
"1");
map.put("email",
"qq@qq.com");
第一种用for循环Java代码
for(Map.Entry
entry:map.entrySet()){System.out.println(entry.getKey()+"--->"+entry.getValue());
}
第二种用迭代Java代码
Set set =
map.entrySet();
Iterator i =
set.iterator();
while(i.hasNext()){
Map.Entry
entry1=(Map.Entry)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);
}
第四种用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);
}