Today on the Internet to see a traversal hashmap to value, with keyset efficiency than with entryset low, and then tested again, found that in fact can not generalize
The following code is executed multiple times, with different values
According to the online example, Hm.put (i, "Thans");
EntrySet: Elapsed Time MS: 16
Keyset: Elapsed time MS: 31
EntrySet: Elapsed Time MS: 47
Keyset: Elapsed Time MS: 15
public void Testhashmap () {
HashMap hm= new HashMap ();
for (int i=0;i<1000;i++) {
Hm.put (i, "abc");
}
System.out.println ("EntrySet:");
Long bs = Calendar.getinstance (). Gettimeinmillis ();
Iterator it = Hm.entryset (). iterator ();
while (It.hasnext ()) {
Entry Entry = (Entry) it.next ();
System.out.print (Entry.getkey () + "[" +entry.getvalue () + "]");
System.out.print ("[" +entry.getvalue () + "]");
}
System.out.println ();
System.out.println ("Elapsed time milliseconds:");
System.out.println (Calendar.getinstance (). Gettimeinmillis ()-bs);
System.out.println ("Take key=0:" +hm.get (0). toString ());
System.out.println ("keyset:");
Long BS2 = Calendar.getinstance (). Gettimeinmillis ();
Iterator it2 = Hm.keyset (). iterator ();
while (It2.hasnext ()) {
Integer KS = (integer) it2.next ();
System.out.print ("[" +hm.get (KS) + "]");
}
System.out.println ();
System.out.println ("Elapsed time milliseconds:");
System.out.println (Calendar.getinstance (). Gettimeinmillis ()-BS2);