This Java HashMap forEach for loop example shows how to iterate HashMap keys, values, or entries using the forEach loop and for loop.
How to Iterate HashMap using forEach and for loop?
How to iterate all keys of HashMap using for loop?
First, get all the keys of the map using the keySet
method and then iterate over them one by one using the enhanced for loop as given in the below example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.HashMap; public class HashMapForEachExample { public static void main(String[] args) { //create new HashMap HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); //add key-value pairs hMapNumbers.put(1, "One"); hMapNumbers.put(2, "Two"); hMapNumbers.put(3, "Three"); /* * Get all keys of HashMap using keySet * method and iterate over them */ for(Integer key : hMapNumbers.keySet()){ System.out.println(key); } } } |
Output
1 2 3 |
1 2 3 |
How to iterate all values of HashMap using for loop?
Get all the values contained in the HashMap object using the values
method and then iterate over them as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.HashMap; public class HashMapForEachExample { public static void main(String[] args) { //create new HashMap HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); //add key-value pairs hMapNumbers.put(1, "One"); hMapNumbers.put(2, "Two"); hMapNumbers.put(3, "Three"); /* * Get all values of HashMap using values * method and iterate over them */ for(String value : hMapNumbers.values()){ System.out.println(value); } } } |
Output
1 2 3 |
One Two Three |
How to iterate all mappings of HashMap using for loop?
Get all the entries contained in the HashMap using the entrySet
method of the HashMap class and then iterate over them using the for loop as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.HashMap; import java.util.Map; public class HashMapForEachExample { public static void main(String[] args) { //create new HashMap HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); //add key-value pairs hMapNumbers.put(1, "One"); hMapNumbers.put(2, "Two"); hMapNumbers.put(3, "Three"); /* * Get all mappings of HashMap using entrySet * method and iterate over them */ for(Map.Entry<Integer, String> entry : hMapNumbers.entrySet()){ //use getKey method to get a key from entry System.out.println("Key: " + entry.getKey()); //use getValue method to get a value from entry System.out.println("Value: " + entry.getValue()); } } } |
Output
1 2 3 4 5 6 |
Key: 1 Value: One Key: 2 Value: Two Key: 3 Value: Three |
How to iterate all mappings of HashMap using forEach? (Java 8 and later)
If you are using Java 8 or later version, you can use the forEach to iterate over all the mappings of the HashMap as given below.
1 2 3 4 5 6 7 8 9 10 11 |
//create new HashMap HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); //add key-value pairs hMapNumbers.put(1, "One"); hMapNumbers.put(2, "Two"); hMapNumbers.put(3, "Three"); hMapNumbers.forEach( (K,V) -> System.out.println( K + " => " + V ) ); |
Output
1 2 3 |
1 => One 2 => Two 3 => Three |
Please visit How to iterate HashMap example to know more ways to iterate over the HashMap in Java.
This example is a part of the HashMap in Java tutorial.
Please let me know your views in the comments section below.