public static void main(String[] args) {
Map map = new HashMap();
map.put("邓超", "孙俪");//替换
map.put("王宝强", "马蓉");//ok
map.put(null, "刘亦菲");//pl
map.put("鹿晗", null);//ok
map.put("lu汉", "刘亦菲");
map.put("沫年", "刘亦菲");//ok
System.out.println(map);
//第一组:先取出所有的Key,通过键值对取出相应的Value
Set keyset = map.keySet();
System.out.println("======I=====");
for (Object key : keyset) {
System.out.println(map.get(key));//这个取出的是Value
}
for (Object key : keyset) {
System.out.println(key);//这个取出的是键值
}
//迭代器
System.out.println("======itit======");
Iterator ito = map.keySet().iterator();
while(ito.hasNext()){
Object key = ito.next();
// System.out.print(key+" ");//输出key的值
System.out.print(map.get(key)+" ");//输出key的value
}
System.out.println("\n===============");
//第二组:把所有的values取出
Collection values = map.values();
System.out.println("======第1组增强for循环======");
for(Object obj: map.values()){
System.out.print(obj +" ");
// System.out.print(values +" ");//可以直接输出values的值
}
System.out.println("\n======第2组增强for循环======");
Iterator ito2 = values.iterator();
while(ito2.hasNext()){
System.out.print(ito2.next()+" ");
}
System.out.println("=======第三组增强for=======");
Set entrySet = map.entrySet();
System.out.println("=========EntrySet的for增强============");
for (Object entry :entrySet) {
Map.Entry m = (Map.Entry) entry;
System.out.print(m.getValue()+" ");
}
System.out.println("\n==========迭代器===========\n");
Iterator ito3 = entrySet.iterator();
while(ito3.hasNext()){
Object entry = ito3.next();
System.out.println(entry);//HashMap$Node ---实现 > Map.Entry (getKey,getValue)
//向下转型 Map.Entry
Map.Entry m = (Map.Entry) entry;
System.out.print(m.getValue()+" ");
}
}
}