本帖最后由 邱成 于 2012-9-10 07:47 编辑
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.Collection;
import java.util.Iterator;
class aaa{
public static void main(String args[]){
HashMap aa=new HashMap();
aa.put("1","java");
aa.put("2","VB");
aa.put("3","VC++");
//取出K和V的collection
Set k1=aa.keySet();
System.out.println("Key:");
Iterator aaaa=k1.iterator();
while(aaaa.hasNext()){
System.out.println(aaaa.next());
}
//取出与V相对应的K
Collection v=aa.values();
System.out.println("Values:");
Iterator bbbb=v.iterator();
while(bbbb.hasNext()){
System.out.println(bbbb.next());
}
//一一对应
Set k3=aa.entrySet();
Iterator cccc=k3.iterator();
while(cccc.hasNext()){
Map.Entry mp=(Map.Entry)cccc.next();
System.out.println(mp.getKey()+":"+mp.getValue());
}
}
}
上述代码打印出的结果为什么Key的顺序是3 2 1而不是1 2 3. |
|