Common methods of traversing map in Java

Source: Internet
Author: User

The following methods apply to any map implementation (HASHMAP, TreeMap, Linkedhashmap, Hashtable, and so on):

Mode one (recommended):

1 //Recommended2 //use entries in the For-each loop to traverse3 //Note: The For-each loop is introduced in Java 5, so the method can only be applied to Java 5 or later. 4 //If you traverse an empty map object, the For-each loop will throw nullpointerexception, so you should always check for null references before traversing. 5 Private Static voidTestMethod1 (Map<integer, string>map) {6 7    //This is the most common and, in most cases, the most desirable way to traverse. Used when the key value is required. 8     for(Map.entry<integer, string>Entry:map.entrySet ()) {9System.out.println ("Key =" + entry.getkey () + ", Value =" +Entry.getvalue ());Ten    } One  A}

Way two:

1     //method Two traverses the keys or values in the For-each loop. 2     //If you only need the keys or values in the map, you can implement the traversal through keyset or values instead of using EntrySet. 3     //This method is slightly better than entryset traversal (10% faster), and the code is cleaner. 4     Private Static voidTESTMETHOD2 (Map<integer, string>map) {5 6         //traverse a key in a map7          for(Integer key:map.keySet ()) {8 9System.out.println ("Key =" +key);Ten  One         } A  -         //traversing values in a map -          for(String value:map.values ()) { the  -System.out.println ("Value =" +value); -  -         } +  -}

Way three:

1     //method Three uses iterator traversal2     //This approach has two benefits, one in the old version of Java, which is the only way to traverse the map. Another benefit is that you can delete the entries by calling Iterator.remove () over the duration, and the other two methods cannot. 3     Private Static voidTestMethod3 (Map<integer, string>map) {4 5Iterator<map.entry<integer, string>> entries =Map.entryset (). iterator ();6 7          while(Entries.hasnext ()) {8 9Map.entry<integer, string> Entry =Entries.next ();Ten  OneSystem.out.println ("Key =" + entry.getkey () + ", Value =" +Entry.getvalue ()); A  -         } -}

Mode four:

1     //method Four, through the key to find the value traversal (low efficiency)2     //rather slow and inefficient3     Private Static voidTESTMETHOD4 (Map<integer, string>map) {4 5          for(Integer key:map.keySet ()) {6 7String value =Map.get (key);8 9System.out.println ("key =" + key + ", Value =" +value);Ten  One         } A}

Common methods of traversing map in Java

Related Article

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.