Six Ways to Traverse HashMap in Java Language

 2 minutes to read

We use a total of six methods to traverse the HashMap. The computational efficiency method is to record the time before the traversal and the end time after the traversal. The time difference is the time used for the traversal. After comparing the time size of the six methods, the smaller the time difference, the higher the efficiency.

import java.util.*;

/**
 * @author hack's home
 * @date 2020-08-22
 */
public class HashMapTest {

    private static HashMap<Integer, String> hashMap;

    static {
        hashMap = new HashMap<>();
        for (int i = 0; i <10000; i++) {
            hashMap.put(i, "hello world");
        }
    }

    public static void main(String[] args) {
        System.out.println(hashMap.size());
        List<Long> list = new ArrayList<>();
        long oneStart = System.currentTimeMillis();
        for (Map.Entry<Integer, String> entry: hashMap.entrySet()) {
            System.out.println("Key = "+ entry.getKey() + ", Value =" + entry.getValue());
        }
        long result = System.currentTimeMillis()-oneStart;
        list.add(result);

        long twoStart = System.currentTimeMillis();
        Iterator<Map.Entry<Integer, String>> it = hashMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Integer, String> entry = it.next();
            System.out.println("key = "+ entry.getKey() +" and value = "+ entry.getValue());
        }
        long result2 = System.currentTimeMillis()-twoStart;
        list.add(result2);

        long threeStart = System.currentTimeMillis();
        for (Integer key: hashMap.keySet()) {
            System.out.println("key= "+ key +" and value= "+ hashMap.get(key));
        }
        long result3 = System.currentTimeMillis()-threeStart;
        list.add(result3);

        long fourStart = System.currentTimeMillis();
        for (String v: hashMap.values()) {
            System.out.println("value= "+ v);
        }

        long result4 = System.currentTimeMillis()-fourStart;
        list.add(result4);

        long fiveStart = System.currentTimeMillis();
        hashMap.forEach((k, v) -> System.out.println("key: "+ k +" value:" + v));
        long result5 = System.currentTimeMillis()-fiveStart;
        list.add(result5);


        long sixStart = System.currentTimeMillis();
        Iterator it1 = hashMap.keySet().iterator();
        while (it1.hasNext()) {
            Integer integer = (Integer) it1.next();
            System.out.println("key= "+ integer +" and value= "+ hashMap.get(integer));
        }
        long result6 = System.currentTimeMillis()-sixStart;
        list.add(result6);

        list.forEach(System.out::println);
        System.out.println(list);
    }
}

The first traversal method uses Map.entrySet to traverse key and value

The second traversal method uses Map.entrySet to traverse key and value using iterator

The third traversal method uses Map.keySet to traverse key and value

The fourth traversal method uses Map.values() to traverse all values, but cannot traverse the key

The fifth traversal method uses Java 8 lambda expression traversal

The sixth traversal method uses Map.keySet to traverse key and value

The result after running is as follows: [97, 45, 51, 39, 112, 45]

They represent the running time of the six modes. You can see that the fourth traversal mode runs the fastest, but it can only traverse the value, so if you traverse the key-value pairs at the same time, the second or sixth method is recommended.