import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class MapDemo {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("1", "value1");
map.put("2", "value2");
map.put("3", "value3");
/**
* foreach循环keySet()
*/
System.out.println("1.通过Map.KeySet遍历key和value");
for (String key : map.keySet()) {
System.out.println(key+"--"+map.get(key));
}
/**
* entrySet() foreach处理大容量数据
*/
System.out.println("2.通过Map.entrySet使用foreach遍历key和value");
for (Map.Entry entry :map.entrySet()) {
System.out.println(entry.getKey()+"--"+entry.getValue());
}
/**
* entrySet() iterator()迭代器循环
*/
System.out.println("3.通过map.entrySet使用iterator遍历key和value");
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
System.out.println(entry.getKey()+"--"+entry.getValue());
}
/**
* 只取value值的循环
*/
System.out.println("4.通过map.values()遍历所有value,但不能遍历key");
for (String v : map.values()) {
System.out.println(v);
}
}
}