package ditu.com;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class TestMap {
public static void main(String[] args) {
//向Map中插入5个人名
Map <Integer,String>map=new HashMap <Integer,String>();
map.put(1,"张飞");
map.put(6,"刘备");
map.put(4,"关羽");
map.put(2,"钟馗");
map.put(3,"达摩");
Map <Integer,String>map2=new HashMap <Integer,String>();
map.put(4,"孙尚香");
map.put(7,"孙悟空");
map.putAll(map2);
System.out.println(map.get(4));
//以上是map的基本操作
//如何遍历map中所有元素?
//1.先获得键的集合
Set <Integer>set=map.keySet();
for(Integer i:set){
System.out.println(i+":"+map.get(i));
}
//2.只获得值(用的比较少)
Collection<String>c=map.values();
for(String cc:c){
System.out.println(cc);
}
//最难理解但效率最高。获得键值对的封装体,Map.Entry
Set<Entry<Integer,String>>s=map.entrySet();
for(Entry<Integer,String> e:s){
System.out.println(e.getKey()+":"+e.getValue());
}
}
}