Regarding the four specific ways to traverse a map in Java, please see the detailed explanation below.
Method 1 This is the most common and in most cases the most desirable way to traverse. Used when both key and value 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 a for-each loop.
If you only need the keys or values in the map, you can traverse through keySet or values instead of using entrySet.
Map<Integer, Integer> map = new HashMap<Integer, Integer>(); //Traverse the keys in the map for (Integer key : map.keySet()) { System.out.println("Key = " + key) ; } //Traverse the values in the map for (Integer value : map.values()) { System.out.println("Value = " + value); }
This method has slightly better performance than entrySet traversal (10% faster), and the code is cleaner.
Method three uses Iterator to traverse
Use generics :
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()); }
Without using 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 approach to keySets and values.
This approach seems redundant but has its advantages. First of all, this is the only way to traverse a map in older versions of Java. Another benefit is that you can call iterator.remove() to delete entries while traversing, which the other two methods cannot. According to the javadoc, if you try to use this method in a for-each traversal, the results are unpredictable.
From a performance perspective, this method is similar to the performance of for-each traversal (ie method 2).
Method 4: Traverse by finding values 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);
As an alternative to method one, this code looks cleaner; but it's actually quite slow and inefficient. Because getting the value from the key is a time-consuming operation (compared to method 1, this method is 20% to 200% slower in different Map implementations). If you have FindBugs installed, it will check and warn you about inefficient traversals. So try to avoid using it.
Summarize
If you only need keys or values, use method two. If the language version you are using is lower than Java 5, or you plan to delete entries during traversal, you must use method three. Otherwise, use method one (both key and value).