Three map traversal methods: 1. Use keyset traversal, while loop; 2. Use entryset traversal, while loop; 3. Use for loop traversal. The following is the test code. I love to read the code most. It is useless to read more code.
J2ee_api:
Http://download.csdn.net/source/2921112
Java interview book _ pdf version:
Http://download.csdn.net/source/3563084
JSP technical knowledge points:
Http://download.csdn.net/source/3567902
Import Java. util. *; public class maptraverse {public static void main (string [] ARGs) {string [] STR = {"I love you", "you love he ", "he love her", "she love me"}; Map <integer, string> M = new hashmap (); For (INT I = 0; I <Str. length; I ++) {M. put (I, STR [I]);} system. out. println ("The following is the result output using usewhilesentence ():"); usewhilesentence (m); system. out. println ("The following is the result output using usewhilesentence2 ():"); usewhilesentence2 (m); system. out. println ("The following is the result output using useforsentence () method:"); useforsentence (m);} public static void usewhilesentence (Map <integer, string> m) {set S = (set <integer>) M. keyset (); iterator <integer> it = S. iterator (); int key; string value; while (it. hasnext () {key = it. next (); value = (string) M. get (key); system. out. println (Key + ": \ t" + value) ;}} public static void usewhilesentence2 (MAP m) {set S = m. entryset (); iterator <map. entry <integer, string> it = S. iterator (); map. entry <integer, string> entry; int key; string value; while (it. hasnext () {entry = it. next (); Key = entry. getkey (); value = entry. getvalue (); system. out. println (Key + ": \ t" + value) ;}} public static void useforsentence (Map <integer, string> m) {int key; string value; For (map. entry <integer, string> entry: M. entryset () {key = entry. getkey (); value = entry. getvalue (); system. out. println (Key + ": \ t" + value );}}}
Thank you for your criticism!