Java traverses Map objects in four ways

  • 2020-04-01 04:19:05
  • OfStack

See below for details on the four ways to traverse a map in Java.

This is the most common and in most cases the most desirable way to traverse. Used when both keys and values are required.


 Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 
 for (Map.Entry<Integer, Integer> entry : map.entrySet()) { 
   System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); 
 } 

Method 2 traverses keys or values in the for-each loop.

If you only need keys or values in the map, you can iterate through keySet or values instead of using entrySet.


 Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 
 //Iterate over the keys in the map
 for (Integer key : map.keySet()) { 
   System.out.println("Key = " + key); 
 } 
 //Iterate over the values in the map
 for (Integer value : map.values()) { 
   System.out.println("Value = " + value); 
 } 

This method is slightly better in performance (10% faster) than entrySet traversal, and the code is cleaner.

Method 3 is iterated using an Iterator

Use generic :


 Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 
 Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator(); 
 while (entries.hasNext()) { 
   Map.Entry<Integer, Integer> entry = entries.next(); 
   System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); 
 } 

No generics:


 Map map = new HashMap(); 
 Iterator entries = map.entrySet().iterator(); 
 while (entries.hasNext()) { 
   Map.Entry entry = (Map.Entry) entries.next(); 
   Integer key = (Integer)entry.getKey(); 
   Integer value = (Integer)entry.getValue(); 
   System.out.println("Key = " + key + ", Value = " + value); 
 } 

You can also apply the same method to keySet and values.

This seems redundant but has its advantages. First, this is the only way to traverse a map in older versions of Java. Another benefit is that you can call iterator.remove() on the traversal to remove entries, while the other two methods cannot. According to javadoc, if you try this method in the for-each traversal, the results are unpredictable.

In terms of performance, this method is similar to the performance of the for-each traversal (that is, method 2).

Method 4. Traversal by finding the value by key (low efficiency)


 Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 
 for (Integer key : map.keySet()) { 
   Integer value = map.get(key); 
   System.out.println("Key = " + key + ", Value = " + value); 

This code looks cleaner as an alternative to method 1; But it's actually quite slow and inefficient. Because taking values from keys is a time-consuming operation (this method is 20% to 200% slower in different Map implementations than method 1). If you install FindBugs, it checks and warns you about inefficient traversal. So try to avoid it.

conclusion

Use method two if you only need keys or values. If you are using a language version lower than Java 5, or if you intend to delete entries on the traversal, you must use method 3. Otherwise use method one (both keys and values).


Related articles: