Map map = new HashMap();
map.put("m1", "a");
map.put("m2", "b");
map.put("m3", "c");
map.put("m4", "d");
map.put("m5", "e");
map.put("m6", "f");
map.put("m7", "g");
map.put("m8", "h")
rt....
map.put("m1", "a");
map.put("m2", "b");
map.put("m3", "c");
map.put("m4", "d");
map.put("m5", "e");
map.put("m6", "f");
map.put("m7", "g");
map.put("m8", "h")
rt....
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
map.values()
for(Iterator it = map.entrySet().iterator(); it.hasnext();)
{
Entry <String, String> entry = (Entry <String, String>)it.next();
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
给个简单的例子
int []a=new int[10];
for(int b:a){
}
表示对数组a进行遍历,b表示当前遍历的对象
map.entrySet()返回一个集合.
{
Entry entry = (Entry)it.next();
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}方式2Iterator it=map.keySet().iterator();
while(it.hasNext()){
String key=(String)it.next();
System.out.println(key);
}
Iterator values=map.values().iterator();
while(values.hasNext()){
Object value=values.next();
System.out.println(value);
}