Java traversal Map summary

It is a common operation to traverse map in the development process. For the convenience of the future, it is summarized here.

1. Data preparation

Map<String, Object> map = new HashMap<>();
map.put("2018", "France");
map.put("2014", "Germany");
map.put("2010", "Spain");
map.put("2006", "Italy");
map.put("2002", "Brazil");

2.keySet mode

Method 1: the keySet first obtains the key of the map, and then obtains the corresponding value according to the key.

    /**
     * Traverse key and value through Map.keySet
     */
    public static void test1(Map<String, Object> map) {

        for (String key : map.keySet()) {
            System.out.println("key==>" + key + ",Corresponding value==>" + map.get(key));
        }

    }

Method 2: use the iterator to get the key and the corresponding value

    /**
     * Traversing key and value with Iterator through keySet
     */
    public static void test3(Map<String, Object> map) {

           Iterator<String> iter = map.keySet().iterator();
               while(iter.hasNext()){
               String key=iter.next();
               String value = map.get(key);        
               System.out.println("key==>" + key + ",Corresponding value==>" + value );
           }
        
    }

3.values() method

Traverse all values through values(), but not key

    /**
     * Traverse all value s through Map.values(), but not key
     */
    public static void test2(Map<String, Object> map) {
 
        for (Object value : map.values()) {
            System.out.println("map Of value value==>" + value);
        }
        
    }

4.map.entrySet() method

Method 1: loop each pair of key value pairs in the map, and then get the key and value

    /**
     * Traverse key and value through Map.entrySet, recommended for large capacity
     */
    public static void test4(Map<String, Object> map) {

        for (Map.Entry<String, Object> entry : map.entrySet()) {
            System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue());
        }
        
    }

Method 2: use iterator to get key

    /**
     * Traverse key and value with Iterator through Map.entrySet
     */
    public static void test3(Map<String, Object> map) {

        Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue());
        }
       
    }

values() returns the set of V values, a list set object; keySet() returns the set of K values, a set set object; entrySet() returns the set of K-V values.

5. Java 8 traversal mode

    public static void test1(Map<String, Object> map) {
        //Traverse key and value through Map.keySet
        map.keySet().forEach(key->
                System.out.println("java8 key==>" + key + ",Corresponding value==>" + map.get(key))
        );

        //Map.values() traverses all value s, but not key
        map.values().forEach(System.out::println); // Equivalent to map. Values(). Foreach (value - > system. Out. Println (value));

        //Traverse key and value with Iterator through Map.entrySet
        map.entrySet().iterator().forEachRemaining(item -> System.out.println("key:value=" + item.getKey() + ":" + item.getValue()));

        //Traverse key and value through Map.entrySet, recommended for large capacity
        map.entrySet().forEach(entry -> System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue()));

        //Through k,v traversal, Java 8 unique
        map.forEach((key, value) -> System.out.println("key:value = " + key + ":" + value));
    }

Summary: when the number of K-V key value pairs is small, the efficiency of traversing keySet and entrySet is not much different, but the efficiency of traversing entrySet is much higher than that of keySet in large amount of data, mainly because the keySet traverses twice, one is to turn into an Interator object, the other is to take the value corresponding to the key from the hashMap, while the entrySet traverses only once and puts both the key and value into E Ntry. forEach traversal set appears in Java 8, and its efficiency is not as high as entrySet.

Keywords: Java

Added by danesc on Tue, 05 Nov 2019 17:36:14 +0200