Conversion] Two Methods of hashmap traversal in Java: http://www.javaweb.cc/language/java/032291.shtmlfirst: Map map = new hashmap (); iterator iter = map. entryset (). iterator (); While (ITER. hasnext () {map. entry entry = (map. entry) ITER. next (); object key = entry. getkey (); object val = entry. getvalue ();} is highly efficient. You must use this method later! Type 2: Map map = new hashmap (); iterator iter = map. keyset (). iterator (); While (ITER. hasnext () {object key = ITER. next (); object val = map. get (key) ;}efficiency is low. Try to use it less in the future! There are two common methods for hashmap traversal: Using keyset and entryset for traversal, but the traversal speed of the two is different. See the example below: public class hashmaptest {public static void main (string [] ARGs )... {hashmap = new hashmap (); For (INT I = 0; I <1000; I )... {hashmap. put ("" I, "thanks");} Long BS = calendar. getinstance (). gettimeinmillis (); iterator = hashmap. keyset (). iterator (); While (iterator. hasnext ())... {system. out. print (hashmap. get (I Terator. next ();} system. out. println (); system. out. println (calendar. getinstance (). gettimeinmillis ()-BS); listhashmap ();} public static void listhashmap ()... {Java. util. hashmap = new Java. util. hashmap (); For (INT I = 0; I <1000; I )... {hashmap. put ("" I, "thanks");} Long BS = calendar. getinstance (). gettimeinmillis (); Java. util. iterator it = hashmap. entryset (). iterator (); While (it. Hasnext ())... {Java. util. map. entry entry = (Java. util. map. entry) it. next (); // entry. getkey () returns the key corresponding to this entry // entry. getvalue () returns the value system corresponding to this item. out. print (entry. getvalue ();} system. out. println (); system. out. println (calendar. getinstance (). gettimeinmillis ()-BS) ;}} the keyset is actually traversed twice, one is converted to iterator, and the value of the key is retrieved from the hashmap at one time. However, entryset only traverses the data for the first time. It puts both the key and value into the entry, so it's faster. Two Methods of hashmap traversal in Java (this tutorial is only for research and learning, does not represent the point of view of Java's Chinese Network/