import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/*
* Map遍历元数
*/
public class MapTest {
public static void main(String[] args) {
Map<Integer,String> map =new HashMap<Integer,String>();
//map.put(key, value);
map.put(1, "a");
map.put(2, "b");
map.put(3, "c");
System.out.println("map的长度为:"+map.size());
//方法1:通过Map.key遍历KEY的value
System.out.println("-----方法1:通过MAP.KEY遍历KEY的value-----");
for(Integer key:map.keySet()) {
String list =map.get(key);
System.out.println(key+":"+list);
}
//方法2:通过Map.entrySet使用iterator遍历key和value
System.out.println("-----方法2:通过Map.entrySet使用iterator遍历key和value-----");
Iterator<Map.Entry<Integer, String>> key =map.entrySet().iterator();
while(key.hasNext()) {
Map.Entry<Integer, String> entry =key.next();
System.out.println(entry.getKey()+":"+entry.getValue());
}
//方法3:通过Map.entrySet遍历key和value
System.out.println("-----方法3:通过Map.entrySet遍历key和vale-----");
for(Map.Entry<Integer, String> entry:map.entrySet()) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
//方法4:通过Map.values()遍历value,但是不能遍历key
System.out.println("-----方法4:通过Map.values()遍历value,但是不能遍历key-----");
for(String v:map.values()) {
System.out.println(v);
}
//方法5:输出key
System.out.println("-----方法5:通过map.keySet()遍历key,但是不能遍历value-----");
for(Integer k:map.keySet()) {
System.out.println(k);
}
}
}