SoFunction
Updated on 2025-05-08

6 common ways to traverse HashMap in Java

In Java, there are many ways to traverse HashMap. Here are several common traversal methods to you.

1. Use keySet() to traverse the key, and then get the value through the key

  • Create a HashMap object and add some key-value pairs to it.
  • Use the () method to get a collection of all keys in a HashMap.
  • Loop through this key collection via for-each.
  • In the loop body, use the (key) method to obtain the corresponding value based on the current key and print out the key and value.
package ;

import ;
import ;

public class Test03 {
    public static void main(String[] args) {

        // Create a HashMap and add elements        Map<String, String> map = new HashMap<>();
        ("Zhang Fei", "male");
        ("Sun Shangxiang", "female");
        ("Guan Yu", "male");
        ("Diao Chan", "female");

        // traversal key collection        for (String key : ()) {
            // Get the value according to the key            String value = (key);
            ("Key: " + key + ", Value: " + value);
        }
    }
}

2. Use entrySet() to traverse key-value pairs

  • First create a HashMap object and add a key-value pair.
  • Use the () method to get a collection containing all key-value pairs.
  • Iterate through this collection through the for-each loop, and each loop gets an object.
  • In the loop body, use the () method to get the keys, use the () method to get the values, and print them out.
package ;

import ;
import ;

public class Test03 {
    public static void main(String[] args) {

        // Create a HashMap and add elements        Map<String, String> map = new HashMap<>();
        ("Zhang Fei", "male");
        ("Sun Shangxiang", "female");
        ("Guan Yu", "male");
        ("Diao Chan", "female");

        // traverse key-value pair collection        for (<String, String> entry : ()) {
            // Get key            String key = ();
            // Get the value            String value = ();
            ("Key: " + key + ", Value: " + value);
        }
    }
}

3. Use the forEach() method (Java 8 and above)

  • Create a HashMap and add elements.
  • Call the () method and pass a Lambda expression. Parameters key and value of Lambda expressions
    Represents the keys and values ​​currently traversed.
  • In the method body of a Lambda expression, print the keys and values.
package ;

import ;
import ;

public class Test03 {
    public static void main(String[] args) {

        // Create a HashMap and add elements        Map<String, String> map = new HashMap<>();
        ("Zhang Fei", "male");
        ("Sun Shangxiang", "female");
        ("Guan Yu", "male");
        ("Diao Chan", "female");

        // Use the forEach method to traverse        ((key, value) -> {
            ("Key: " + key + ", Value: " + value);
        });
    }
}

4. Use iterator to traverse

  • Create a HashMap and add elements.
  • Use the ().iterator() method to get the iterator of the set of key-value pairs.
  • Use the while loop combination () method to determine whether there is another element.
  • In the loop body, use the () method to get the next object, then get the keys and values ​​and print the output.
package ;

import ;
import ;
import ;

public class Test03 {
    public static void main(String[] args) {

        // Create a HashMap and add elements        Map<String, String> map = new HashMap<>();
        ("Zhang Fei", "male");
        ("Sun Shangxiang", "female");
        ("Guan Yu", "male");
        ("Diao Chan", "female");

        // Get the iterator for the set of key-value pairs        Iterator<<String, String>> iterator = ().iterator();

        // Use iterator to traverse        while (()) {
            <String, String> entry = ();
            String key = ();
            String value = ();
            ("Key: " + key + ", Value: " + value);
        }
    }
}

5. Iterate through all keys

Note that it is just traversing the key value, that is, the key value, the code is as follows:

package ;

import ;
import ;

public class Test03 {
    public static void main(String[] args) {

        // Create a HashMap and add elements        Map<String, String> map = new HashMap<>();
        ("Zhang Fei", "male");
        ("Sun Shangxiang", "female");
        ("Guan Yu", "male");
        ("Diao Chan", "female");

        // Iterate through all keys        for (String key : ()) {
            ("key : " + key);
        }
    }
}

6. Iterate through all values

Note that it is just traversing the value of the key-value pair, that is, the code is as follows:

package ;

import ;
import ;

public class Test03 {
    public static void main(String[] args) {

        // Create a HashMap and add elements        Map<String, String> map = new HashMap<>();
        ("Zhang Fei", "male");
        ("Sun Shangxiang", "female");
        ("Guan Yu", "male");
        ("Diao Chan", "female");

        // Iterate through all keys        for (String key : ()) {
            ("value: " + key);
        }
    }
}

Summarize

This is the end of this article about 6 common ways to traverse HashMap in Java. For more related content on traversing HashMap, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!