The first is a small example of traversal:
public static void Main (string[] args) {
//TODO auto-generated method Stub
map<string, string> maps = new H Ashmap<string, string> ();
Maps.put ("A", "111a");
Maps.put ("222", "222b");
Maps.put ("333", "333c");
Maps.put ("444", "444d");
Maps.put ("555", "555e");
Maps.put ("666", "666f");
For (String Str:maps.keySet ()) {
System.out.println (str + ":" + maps.get (str));
}
System.out.println ("--------------");
For (entry<string, string> str:maps.entrySet ()) {
System.out.println (str + " " + str.getkey () + ":" + str.) GetValue ());
}
As for the performance of the two:
The test found that the performance of the second approach is usually one-fold higher than the first method.
Examples are as follows:
Import Java.util.Calendar;
Import Java.util.Date;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map.Entry; /** * Test keyset () and EntrySet () Iteration time * Keyset (): After iteration, the key * EntrySet () can only be taken via get (): After iteration, E.getkey (), E.getvalue () take key and value. Returns the entry interface * The final description of the keyset () is slower than the EntrySet () a lot faster. It seems to be considered later with EntrySet () * @author YL * @date 2009.6.10/public class Hashmaptest {public static void main (string[] Ar
GS) {hashmap<string,string> kmap = new hashmap<string,string> ();
hashmap<string, string> emap = new hashmap<string, string> ();
Load data for (int i = 0; i < 1000 i++) {kmap.put ("" +i, "YL");
for (int i = 0; i < 1000 i++) {emap.put ("" +i, "ZT");
Long stimes = System.currenttimemillis ();
Long ctimes = Calendar.getinstance (). Gettimeinmillis ();
Long dtimes = new Date (). GetTime ();
Initial time here I used three kinds of value methods to find System.currenttimemillis (), the most direct method of System.out.println (stimes+ "" +ctimes+ "" +dtimes); iterator&Lt
string> ktor = Kmap.keyset (). iterator ();
while (Ktor.hasnext ()) {System.out.println (Ktor.next ());
Long stimes1 = System.currenttimemillis ();
Long ctimes1 = Calendar.getinstance (). Gettimeinmillis ();
Long dtimes1 = new Date (). GetTime ();
End world and also entryset start Time System.out.println ((stimes1-stimes) + "" + (Ctimes1-ctimes) + "" + (Dtimes1-dtimes));
System.out.println (stimes1+ "" +ctimes1+ "" +dtimes1);
iterator<entry<string, string>> itor = Emap.entryset (). iterator ();
while (Itor.hasnext ()) {entry<string, string> e = Itor.next ();
System.out.println (E.getkey ());
System.out.println (E.getvalue ());
Long stimes2 = System.currenttimemillis ();
Long ctimes2 = Calendar.getinstance (). Gettimeinmillis ();
Long dtimes2 = new Date (). GetTime ();
System.out.println (stimes2+ "" +ctimes2+ "" +dtimes2);
System.out.println ((stimes2-stimes1) + "" + (CTIMES2-CTIMES1) + "" + (dtimes2-dtimes1));
}
}