Common Methods for Traversing Map in Java, java for Traversing map

Source: Internet
Author: User

Common Methods for Traversing Map in Java, java for Traversing map

The following method applies to any map implementation (HashMap, TreeMap, LinkedHashMap, Hashtable, and so on ):

Method 1 (recommended ):

1 // recommendation 2 // use entries in the for-each loop to traverse 3 // note: the for-each loop is introduced in Java 5, so this method can only be applied to java 5 or a later version. 4 // If you traverse an empty map object, the for-each loop will throw NullPointerException. Therefore, you should always check for empty references before traversing. 5 private static void testMethod1 (Map <Integer, String> map) {6 7 // This is the most common and is also the most desirable Traversal method in most cases. Used when key values are required. 8 for (Map. entry <Integer, String> entry: map. entrySet () {9 System. out. println ("Key =" + entry. getKey () + ", Value =" + entry. getValue (); 10} 11 12}

Method 2:

1 // method 2 traverse keys or values in the for-each loop. 2 // If you only need the key or value in map, you can use keySet or values to traverse, instead of entrySet. 3 // This method performs better (10% faster) than entrySet traversal, and the code is cleaner. 4 private static void testMethod2 (Map <Integer, String> map) {5 6 // traverse the key 7 for (Integer key: map. keySet () {8 9 System. out. println ("Key =" + key); 10 11} 12 13 // traverse the value of 14 for (String value: map. values () {15 16 System. out. println ("Value =" + value); 17 18} 19 20}

Method 3:

1 // method 3 Use Iterator to traverse 2 // This method has two advantages. First, in the old java version, this is the only way to traverse the map. Another benefit is that you can call iterator. remove () to delete entries over time periods. The other two methods cannot. 3 private static void testMethod3 (Map <Integer, String> map) {4 5 Iterator <Map. entry <Integer, String> entries = map. entrySet (). iterator (); 6 7 while (entries. hasNext () {8 9 Map. entry <Integer, String> entry = entries. next (); 10 11 System. out. println ("Key =" + entry. getKey () + ", Value =" + entry. getValue (); 12 13} 14}

Method 4:

1 // Method 4. Search for values through keys (low efficiency) 2 // relatively slow and inefficient 3 private static void testMethod4 (Map <Integer, String> map) {4 5 for (Integer key: map. keySet () {6 7 String value = map. get (key); 8 9 System. out. println ("Key =" + key + ", Value =" + value); 10 11} 12}

 

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.