This example shows how to iterate through HashMap in Java including iterating over keys, values, and mappings of the HashMap using while loop, for loop and enhanced for loop.
How to iterate over Java HashMap?
We will use the example Employee class and store its objects into Java HashMap.
Let’s first store several Employee objects into the HashMap object.
1 2 3 4 5 6 |
HashMap<String, Employee> employeeMap = new new HashMap<String, Employee>(); employeeMap.put("emp01", new Employee("emp01", "Jason", "IT")); employeeMap.put("emp02", new Employee("emp02", "Aaron", "Supply Chain")); employeeMap.put("emp03", new Employee("emp03", "Oliver", "Marketing")); employeeMap.put("emp04", new Employee("emp04", "Raj", "IT")); |
We have put employee ID as the key and employee object as the value for each mapping stored in the HashMap.
1) Iterate through keys of the HashMap
Use this approach if you are interested only in keys stored in the HashMap. Use the keyset
method of the HashMap class to get the Set view of the stored keys and for loop to iterate over the keys.
1 2 3 4 |
System.out.println("Iterate over HashMap Keys"); for(String strKey : employeeMap.keySet() ){ System.out.println( "Key (Employee Id) : " + strKey ); } |
Output
1 2 3 4 5 |
Iterate over HashMap Keys Key (Employee Id) : emp01 Key (Employee Id) : emp03 Key (Employee Id) : emp02 Key (Employee Id) : emp04 |
2) Iterate through values of the HashMap
Use this approach if you are interested only in the values stored in the HashMap. Use the values
method of the HashMap class to get the Collection view of the stored values and a for loop to iterate over the values.
1 2 3 4 |
System.out.println("Iterate over HashMap Values"); for(Employee employee : employeeMap.values() ){ System.out.println( "Value (Employee) : " + employee ); } |
Output
1 2 3 4 5 |
Iterate over HashMap Values Value (Employee) : [emp01 : Jason : IT] Value (Employee) : [emp03 : Oliver : Marketing] Value (Employee) : [emp02 : Aaron : Supply Chain] Value (Employee) : [emp04 : Raj : IT] |
3) Iterate through keys and values of the HashMap
Use this approach if you are interested in retrieving both keys and values stored in the HashMap. Use the entrySet
method of the HashMap to get the Set view of stored mappings in the form of Map.Entry
object and use a for loop to iterate over the key-values.
1 2 3 4 |
System.out.println("Iterate over HashMap Keys and Values"); for(Map.Entry<String, Employee> entry : employeeMap.entrySet()){ System.out.println( entry.getKey() + " => " + ": " + entry.getValue() ); } |
Output
1 2 3 4 5 |
Iterate over HashMap Keys and Values emp01 => : [emp01 : Jason : IT] emp03 => : [emp03 : Oliver : Marketing] emp02 => : [emp02 : Aaron : Supply Chain] emp04 => : [emp04 : Raj : IT] |
Important Note: If you are using Java version 1.4 or lower, the enhanced for each loop will not work since it was introduced in Java 1.5. Also, Generics was introduced in Java version 1.5. Use the following code to iterate through HashMap instead.
Using a while loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
System.out.println("Iterate over HashMap Keys and Values"); Map.Entry empEntry = null; Iterator itrEmployee = employeeMap.entrySet().iterator(); while(itrEmployee.hasNext()){ empEntry = (Map.Entry) itrEmployee.next(); String strKey = (String) empEntry.getKey(); Employee empObject = (Employee) empEntry.getValue(); System.out.println(strKey + " => " + ": " + empObject); } |
Using for loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
System.out.println("Iterate over HashMap Keys and Values"); Map.Entry empEntry = null; for( Iterator itrEmployee = employeeMap.entrySet().iterator(); itrEmployee.hasNext(); ){ empEntry = (Map.Entry) itrEmployee.next(); String strKey = (String) empEntry.getKey(); Employee empObject = (Employee) empEntry.getValue(); System.out.println(strKey + " => " + ": " + empObject); } |
Please also note that the HashMap does not guarantee the order of elements returned. That means the returned values might not be in the same order in which they are inserted (that is why you might see different output)
Also See: How to iterate through List in Java.
This example is a part of the Java HashMap tutorial.
Please let me know your views in the comments section.