import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry; /**
* <p>遍历Map集合</p>
* @author:[email protected]
* @date:2017-5-30
*/
public class Test {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("username", "yujiwei");
map.put("password", "12345");
map.put("address", "hangzhou");
map.put("love", "编程");
//1.获取所有的key
for(String key : map.keySet()){//返回的是map的key值
String value = map.get(key);//通过key取value
System.out.println("key = " + key + ",value = " + value);
} System.out.println("----------------------------------"); //2.通过map.entrySet的iterator来遍历Map集合
Iterator<Entry<String, String>> it = map.entrySet().iterator();
while(it.hasNext()){
Entry<String, String> entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
} System.out.println("----------------------------------"); //3.通过Map.Entry来遍历Map集合
for(Map.Entry<String, String> entry : map.entrySet()){
System.out.println("key= " + entry.getKey() + " and value= "+ entry.getValue());
}
}
}