SoFunction
Updated on 2025-03-08

9 ways to traverse hashMap in Java

In Java development, hashMap is a very important container class and stores key-value pairs (key, value).

HashMap inherits AbstractMap, implements Map, Cloneable, and Serializable interfaces, non-thread-safe classes, but is efficient. HashMap allows null and null values, allows value to be repeated, but does not allow key to be repeated. HashMap has two parameters that affect its performance, the initial capacity and the loading factor. When the number of entries in the hash table exceeds the product of the loading factor and the current capacity, the hash table needs to be refreshed and the internal data structure is rebuilt. The capacity is expanded to twice the previous one. The default value of the loading factor is 0.75.

HashMap traversal method

The first type is to traverse the entrySet key-value pair set of HashMap, obtain the key-value pair set through () and traverse the set through the iterator Iterator to get the key and value.

public static void main(String[] args) {
        Map<Object, Object> map = new HashMap<>();
        ("caocao","11");
        ("liubei","22");
        ("sunquan","33");
        first(map);

    }

    private static void first(Map<Object, Object> map) {
        Iterator<<Object, Object>> iterator = ().iterator();
        while (()) {
            <Object, Object> entry = ();
            ("key:" + () + ",vaule:" + ());
        }
    }

The second type is to traverse the keySet collection of HashMap, obtain the key collection through (), and traverse the collection through the iterator Iterator to obtain the key and value.

private static void second(Map<Object, Object> map) {
        Iterator<Object> iterator = ().iterator();
        while (()) {
            Object key = ();
            ("key:" + key + ",vaule:" + (key));
        }
    }

The third type is to traverse the values ​​collection of HashMap, obtain the value collection through () and traverse the collection through the iterator Iterator to obtain the key and value.

private static void third(Map<Object, Object> map) {
        Iterator<Object> iterator = ().iterator();
        while (()) {
            Object value = ();
            ("vaule:" + value);
        }
    }

The fourth type is to traverse the entrySet key-value pair set of HashMap, obtain the key-value pair set through () and traverse the set through the for loop to get the key and value.

private static void fourth(Map<Object, Object> map) {
        for (<Object,Object> entry: ()) {
            ("key:" + () + ",vaule:" + ());
        }
    }

The fifth type is to traverse the keySet collection of HashMap, obtain the key collection through (), and traverse the collection through the for loop to get the key and value.

private static void fifth(Map<Object, Object> map) {
        for(Object key : ()) {
            ("key:" + key + ",vaule:" + (key));
        }
    }

The sixth type is to traverse the values ​​set of HashMap, get the value set through () and traverse the set through for loop to get the key and value.

private static void sixth(Map<Object, Object> map) {
        for (Object value : ()) {
            ("vaule:" + value);
        }
    }

The seventh type is to traverse HashMap through Lambda to get the key and value

private static void seventh(Map<Object, Object> map) {
        ((key,value) -> {
            ("key:" + key + ",vaule:" + value);
        });
    }

The eighth type is to obtain the key value pair collection through Lambda traversal() to obtain the key and value.

private static void eighth(Map<Object, Object> map) {
        ().forEach(item -> {
            ("key:" + () + ",vaule:" + ());
        });
    }

The ninth type is to get the key set through Lambda traversal() to get the key and value.

private static void ninth(Map<Object, Object> map) {
        ().forEach(item -> {
            ("key:" + item + ",vaule:" + (item));
        });
    }

The above traversals are mainly Iterator traversal collection methods, for loop traversal collection methods, Lambda traversal collection methods, and different methods are selected according to specific needs. It is safe to loop deletion of data through Iterator method, it is not safe to loop deletion of data through the For method, and it is not safe to loop deletion of data through the Lambda method, and the efficiency of iterator method is also very high.

This is the end of this article about 9 ways to traverse hashMap in Java. For more related Java hashMap traversal content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!