Four Methods of Map traversal in Java, javamap
How to traverse Map objects in Java
Method 1: This is the most common and is also the most desirable Traversal method in most cases. Used when key values are required.
1 Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 2 3 for (Map.Entry<Integer, Integer> entry : map.entrySet()) { 4 5 System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); 6 7 }
Method 2: traverse keys or values in the for-each loop.
If you only need the key or value in map, you can use keySet or values to traverse, instead of entrySet.
1 Map <Integer, Integer> map = new HashMap <Integer, Integer> (); 2 3 // traverse the key 4 5 for (Integer key: map. keySet () {6 7 System. out. println ("Key =" + key); 8 9} 10 11 // traverse the value of 12 13 for (Integer value: map. values () {14 15 System. out. println ("Value =" + value); 16 17}
This method performs better (10% faster) than entrySet traversal, and the code is cleaner.
Method 3 Use Iterator to traverse
Use generic:
1 Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 2 3 Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator(); 4 5 while (entries.hasNext()) { 6 7 Map.Entry<Integer, Integer> entry = entries.next(); 8 9 System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); 10 11 }
Do not use generic:
1 Map map = new HashMap(); 2 3 Iterator entries = map.entrySet().iterator(); 4 5 while (entries.hasNext()) { 6 7 Map.Entry entry = (Map.Entry) entries.next(); 8 9 Integer key = (Integer)entry.getKey(); 10 11 Integer value = (Integer)entry.getValue(); 12 13 System.out.println("Key = " + key + ", Value = " + value); 14 15 }
You can also apply the same method on keySet and values.
This method seems redundant but has its advantages. First, in earlier versions of java, this is the only way to traverse the map. Another benefit is that you can call iterator. remove () to delete entries over time periods. The other two methods cannot. According to javadoc, if you try this method in for-each traversal, the result is unpredictable.
In terms of performance, this method is similar to the performance of for-each traversal (that is, method 2.
Method 4: Search for values through keys (low efficiency)
1 Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 2 3 for (Integer key : map.keySet()) { 4 5 Integer value = map.get(key); 6 7 System.out.println("Key = " + key + ", Value = " + value); 8
As an alternative to method 1, the code looks cleaner, but it is actually quite slow and inefficient. Because the value of the slave key is a time-consuming operation (compared with method 1, this method is 20% ~ slower in different Map implementations ~ 200% ). If you have installed FindBugs, it will check and warn you about which traversal is inefficient. So try to avoid using it.
Summary
If you only need keys or values, use method 2. If you are using a language version earlier than java 5 or intend to delete entries during the time period, you must use method 3. Otherwise, use method 1 (key value required ).