How to traverse a map object in Java
How to iterate over a maps in Java
There are a number of ways to traverse a map in Java. Let's take a look at the most common methods and their pros and cons.
Since all maps in Java implement the map interface, the following methods apply to any map implementation (HASHMAP, TreeMap, Linkedhashmap, Hashtable, and so on)
Method one uses entries in the For-each loop to traverse
This is the most common and, in most cases, the most desirable way to traverse. Used when the key value is required.
[Java]View PlainCopy
- 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 ());
- }
Note: The For-each loop is introduced in Java 5, so the method can only be applied to Java 5 or later. If you traverse an empty map object, the For-each loop will throw nullpointerexception, so you should always check for null references before traversing.
Method Two traverses the keys or values in the For-each loop.
If you only need the keys or values in the map, you can implement the traversal through keyset or values instead of using EntrySet.
[Java]View PlainCopy
- Map<integer, integer> map = new Hashmap<integer, integer> ();
- Traverse a key in a map
- For (Integer Key:map.keySet ()) {
- System.out.println ("key =" + key);
- }
- Traversing values in a map
- For (Integer value:map.values ()) {
- System.out.println ("value =" + value);
- }
This method is slightly better than entryset traversal (10% faster), and the code is cleaner.
Method three uses iterator traversal
Using generics:
[Java]View PlainCopy
- 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 ());
- }
Do not use generics:
[Java]View PlainCopy
- 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 way of looking redundant has its advantages. First, this is the only way to traverse the map in the old version of Java. Another benefit is that you can delete the entries by calling Iterator.remove () over the duration, and the other two methods cannot. According to Javadoc, if you try to use this method in a For-each traversal, the result is unpredictable.
In terms of performance, this method is similar to the performance of for-each traversal (i.e., method two).
Method Four, through the key to find the value traversal (low efficiency)
[Java]View PlainCopy
- 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, the code looks cleaner, but in fact it is quite slow and inefficient. Because the value from the key is a time-consuming operation (compared to method one, the method is slower 20%~200% in different map implementations). If you install FindBugs, it will check and warn you about what is inefficient traversal. So try to avoid it.
Summarize
Use method Two if you only need keys or values. You must use method three if you are using a language version that is lower than Java 5, or if you plan to delete entries over time. Otherwise, use method one (the key value is all).
4 ways to traverse map objects in Java