public class TestMap {
@Test
public void test7(){
Map map = new HashMap<>();
map.put("aa",123);
map.put("bb",1234);
map.put("cc",1245);
//1. 此时输出的全是值 value
System.out.println(map);
//2. 遍历所有的Map中的Key;
Set set = map.keySet();
Iterator it = set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
//3. 用增强for循环遍历 map中所有的值
Collection co = map.values();
for(Object obj: co){
System.out.println(obj);
}
//4 运用迭代器遍历 key和value。
//方法一
Set str = map.entrySet();
Iterator ite = str.iterator();
while(ite.hasNext()){
System.out.println(ite.next());
}
//方法二
Set str1 = map.entrySet();
Iterator it1 = str1.iterator();
while(it1.hasNext()){
Object obj = it1.next();
Map.Entry s2 = (Map.Entry) obj;
System.out.println(s2.getKey()+"......"+s2.getValue());
}
//方法3
Set str2 = map.keySet();
Iterator it2 =str2.iterator();
while(it2.hasNext()){
Object obj = it2.next();
Object value = map.get(obj);
System.out.println(obj+","+value);
}
}
}