示例:
Map maps = new HashMap();
maps.put("1", "A");
maps.put("2", "B");
maps.put("3", "C");
maps.put("4", "D");
//1
Iterator it1 = maps.entrySet().iterator();
while(it1.hasNext()){
Entry entry = (Entry) it1.next();
System.out.println(entry.getKey() + ":" + entry.getValue());
}
System.out.println("-----------------------------------");
//2
Iterator it2 = maps.keySet().iterator();
while(it2.hasNext()){
String key = (String) it2.next();
System.out.println(key + ":" + maps.get(key));
}
Map maps = new HashMap();
maps.put("1", "A");
maps.put("2", "B");
maps.put("3", "C");
maps.put("4", "D");
//1
Iterator it1 = maps.entrySet().iterator();
while(it1.hasNext()){
Entry entry = (Entry) it1.next();
System.out.println(entry.getKey() + ":" + entry.getValue());
}
System.out.println("-----------------------------------");
//2
Iterator it2 = maps.keySet().iterator();
while(it2.hasNext()){
String key = (String) it2.next();
System.out.println(key + ":" + maps.get(key));
}
jdk1.5以上的话,可以用 for each 写法简化以上的写法