map遍历的方式有4种
1、使用for循环遍历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");
2、使用迭代器遍历map;
System.out.println("通过iterator遍历所有的value,但是不能遍历key");
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String, String> next = iterator.next();
System.out.println("key="+next.getKey()+"value="+next.getValue());
}
3、使用keySet迭代遍历map;
System.out.println("通过map.keyset进行遍历key和value");
for (String key:map.keySet()){
System.out.println("key= "+key+" and value= "+map.get(key));
}
4、使用entrySet遍历map。
System.out.println("通过Map.entrySet;")
Set<Map.Entry<String, String>> entries = map.entrySet();
for (Map.Entry<String, String>entry:entries){
String value = entry.getValue();
String key = entry.getKey();
System.out.println("key="+key+"value="+value);
}