https://blog.csdn.net/fuzhongbin/article/details/50521877
public class Main{
public static void main(String[] args) {
Map map=new HashMap();
map.put("王", 1);
map.put("杨", 2);
map.put("赵", 3);
//遍历方式1:遍历key和value
Iterator it=map.entrySet().iterator();
while(it.hasNext()){
Entry entry=(Entry)it.next();
Object key=entry.getKey();
Object value=entry.getValue();
System.out.println(key+":"+value);
}
System.out.println();
//遍历方式2:遍历整个Hashmap
Collection objs=map.entrySet();
for(Iterator iterator=objs.iterator();iterator.hasNext();){
Object obj=iterator.next();
System.out.println(obj);
}
System.out.println();
//遍历方式3:遍历Hashmap的key,根据key获得value
Collection keys=map.keySet();
for(Iterator iterator=keys.iterator();iterator.hasNext();){
Object key=iterator.next();
Object value=map.get(key);
System.out.println(key+":"+value);
}
System.out.println();
//遍历方式4:遍历Hashmap的value
Collection values=map.values();
for(Iterator iterator=values.iterator();iterator.hasNext();){
Object value=iterator.next();
System.out.println(value);
}
}
output:
赵:3
王:1
杨:2
赵=3
王=1
杨=2
赵:3
王:1
杨:2
3
1
2