Three map traversal methods in Java

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.