public class HashMapSet {
public static void main(String [] ags){
Map m = new HashMap();
m.put("w", "111");
m.put("h", "222");
m.put("g", "333");
m.put("d", "444");
m.put("s", "555");
System.out.println("--------------------遍历key和value----------------------");
//entrySet()方法用于在获得关键字的同时也获得值该方法返回一个Map.Entry实例后的对象集
//该方法和keySet()方法相比 keySet每次只获得关键字 获得关键字以后 再返回获得值 比较麻烦
//同时Map.Entry方法 也提供了两个方法 getKey();getValue()
for(Iterator iter = m.entrySet().iterator();iter.hasNext();){
Map.Entry element = (Map.Entry)iter.next();
Object strKey = element.getKey();
Object strValue = element.getValue();
System.out.println(strKey+"/"+strValue);
}
System.out.println();
System.out.println("--------------------遍历整个HashMap----------------------");
//Collection 类是Map和Set接口的父类
Collection objs = m.entrySet();
for (Iterator iterator=objs.iterator(); iterator.hasNext();){
Object obj = iterator.next();
System.out.println(obj);
}
System.out.println();
System.out.println("--------------------遍历HashMap的key----------------------");
Collection keys = m.keySet();
for (Iterator iterator=keys.iterator(); iterator.hasNext();){
Object key = iterator.next();
System.out.println(key);
}
System.out.println();
System.out.println("--------------------遍历HashMap的value----------------------");
Collection values = m.values();
for (Iterator iterator=values.iterator(); iterator.hasNext();){
Object value = iterator.next();
System.out.println(value);
}
}
}