How to loop a Map in Java or How many ways to loop Map in Java are most important questions in any Java interview. It’s require a developer to list down the ways of loop a Map in Java. This post will show you five different ways of traversing a Map in java.
1st way to loop a HashMap
1 2 3 |
for (String key : books.keySet()) { System.out.println("Key= " + key + " Value= " + books.get(key)); } |
2nd way to loop a HashMap
1 2 3 4 5 6 |
Iterator<String> keySet = books.keySet().iterator(); while (keySet.hasNext()) { String string = (String) keySet.next(); System.out.println("Key= " + string + " Value= " + books.get(string)); } |
3rd way to loop a HashMap
1 2 3 4 |
Set<Map.Entry<String, String>> entrySets = books.entrySet(); for (Entry<?, ?> entry : entrySets) { System.out.println("Key= " + entry.getKey() + " Value= " + entry.getValue()); } |
4th way to loop a HashMap
1 2 3 4 5 6 7 |
Iterator<Map.Entry<String, String>> entrySets = books.entrySet().iterator(); while (entrySets.hasNext()) { Map.Entry<String, String> entry = (Map.Entry<String, String>) entrySets .next(); System.out.println("Key= " + entry.getKey() + " Value= " + entry.getValue()); } |
5st way to loop a HashMap
Java 8 only, forEach and Lambda
1 |
books.forEach((k,v)->System.out.println("Key : " + k + " Value : " + v)); |
6st way to loop a HashMap
Java 8 only, Stream API
1 |
books.entrySet().stream().forEach(e -> System.out.println("Key : " + e.getKey() + " Value : " + e.getValue())); |
7st way to loop a HashMap
Java 8 only, Stream API Parallel
1 |
books.entrySet().stream().parallel().forEach(e -> System.out.println("Key : " + e.getKey() + " Value : " + e.getValue())); |
Create a demonstration program
Above we just show you how to loop a Map in Java and how many ways to loop Map in Java. Now, we are creating a main class to run the above code.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
package com.javabycode.collections; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class LoopMapExample { public static void main(String[] args) { Map<String, String> books = new HashMap<String, String>(); books.put("Maths", "88"); books.put("Economics", "128"); books.put("sociology", "224"); // 1st way to loop a HashMap forEach(books); // 2nd way to loop a HashMap iterator(books); // 3rd way to loop a HashMap entrySet(books); // 4th way to loop a HashMap entrySetIte(books); // 5th way, Java 8 only, forEach and Lambda forEachJava8(books); // 6th way, Java 8 only, forEach and Lambda streamAPI8(books); // 5th way, Java 8 only, forEach and Lambda streamAPI8Parallel(books); } // 1st way to loop a HashMap public static void forEach(Map<String, String> books) { System.out.println("1st way to loop a HashMap"); for (String key : books.keySet()) { System.out.println("Key= " + key + " Value= " + books.get(key)); } } // 2nd way to loop a HashMap public static void iterator(Map<String, String> books) { System.out.println("2st way to loop a HashMap"); Iterator<String> keySet = books.keySet().iterator(); while (keySet.hasNext()) { String string = (String) keySet.next(); System.out.println("Key= " + string + " Value= " + books.get(string)); } } // 3rd way to loop a HashMap public static void entrySet(Map<String, String> books) { System.out.println("3st way to loop a HashMap"); Set<Map.Entry<String, String>> entrySets = books.entrySet(); for (Entry<?, ?> entry : entrySets) { System.out.println("Key= " + entry.getKey() + " Value= " + entry.getValue()); } } // 4th way to loop a HashMap public static void entrySetIte(Map<String, String> books) { System.out.println("4st way to loop a HashMap"); Iterator<Map.Entry<String, String>> entrySets = books.entrySet().iterator(); while (entrySets.hasNext()) { Map.Entry<String, String> entry = (Map.Entry<String, String>) entrySets .next(); System.out.println("Key= " + entry.getKey() + " Value= " + entry.getValue()); } } //5st way to loop a HashMap //Java 8 only, forEach and Lambda public static void forEachJava8(Map<String, String> books){ //Java 8 only, forEach and Lambda System.out.println("5st way to loop a HashMap, Java 8 only"); books.forEach((k,v)->System.out.println("Key : " + k + " Value : " + v)); } //6st way to loop a HashMap //Java 8 only, Stream API public static void streamAPI8(Map<String, String> books){ System.out.println("6st way to loop a HashMap, Java 8 Stream API"); books.entrySet().stream().forEach(e -> System.out.println("Key : " + e.getKey() + " Value : " + e.getValue())); } //7st way to loop a HashMap //Java 8 only, Stream API Parallel public static void streamAPI8Parallel(Map<String, String> books){ System.out.println("6st way to loop a HashMap, Java 8 Stream API Parallel"); books.entrySet().stream().parallel().forEach(e -> System.out.println("Key : " + e.getKey() + " Value : " + e.getValue())); } } |
Run above main and produce the outpout like below screen shoot:
That’s it on the How to loop a Map in Java tutorial. If you are interesting about sorting a map, please visit How to sort Hashmap by key in Java