Following are some of the ways to traverse HashMap in
Java.
a. Using the
entrySet method.
/* Method 1 to traverse */ public static void traverseMapUsingEntrySet(Map<Integer, String> map) { Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry<Integer, String> entry = it.next(); System.out.println(entry.getKey() + " = " + entry.getValue()); } }
b. By using keySet
method and for-each loop
/* Method 2 to traverse */ public static void traverseMapUsingKeySet(Map<Integer, String> map) { Set<Integer> keys = map.keySet(); for (Integer key : keys) { System.out.println(key + " = " + map.get(key)); } }
c. Using for-each
loop and entrySet
/* Method 3 to traverse */ public static void traverseMapUsingForEach(Map<Integer, String> map) { for (Map.Entry<Integer, String> entry : map.entrySet()) { System.out.println(entry.getKey() + " = " + entry.getValue()); } }
d. Using keyset
iterator
/* Method 4 to traverse */ public static void traverseMapUsingKeySetIter(Map<Integer, String> map) { Set<Integer> keys = map.keySet(); Iterator<Integer> iter = keys.iterator(); while (iter.hasNext()) { int key = iter.next(); System.out.println(key + " = " + map.get(key)); } }
e. Using streams
Streams are introduced in Java8, you can go through
following link to know more about Java8 features and streams.
/* Method 5 to traverse */ public static void traverseMapUsingStreams(Map<Integer, String> map) { map.entrySet().stream().forEach(System.out::println); }
Following is the
complete working application.
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class HashMapTraversalDemo { /* Method 1 to traverse */ public static void traverseMapUsingEntrySet(Map<Integer, String> map) { Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry<Integer, String> entry = it.next(); System.out.println(entry.getKey() + " = " + entry.getValue()); } } /* Method 2 to traverse */ public static void traverseMapUsingKeySet(Map<Integer, String> map) { Set<Integer> keys = map.keySet(); for (Integer key : keys) { System.out.println(key + " = " + map.get(key)); } } /* Method 3 to traverse */ public static void traverseMapUsingForEach(Map<Integer, String> map) { for (Map.Entry<Integer, String> entry : map.entrySet()) { System.out.println(entry.getKey() + " = " + entry.getValue()); } } /* Method 4 to traverse */ public static void traverseMapUsingKeySetIter(Map<Integer, String> map) { Set<Integer> keys = map.keySet(); Iterator<Integer> iter = keys.iterator(); while (iter.hasNext()) { int key = iter.next(); System.out.println(key + " = " + map.get(key)); } } /* Method 5 to traverse */ public static void traverseMapUsingStreams(Map<Integer, String> map) { map.entrySet().stream().forEach(System.out::println); } public static void main(String ptr[]) { Map<Integer, String> hashMap = new HashMap<>(); hashMap.put(1, "Hari Krishna"); hashMap.put(2, "Jaideep"); hashMap.put(3, "Sailaja"); hashMap.put(4, "Sankalp"); System.out.println("Traverse Using the entrySet"); System.out.println("**************************"); traverseMapUsingEntrySet(hashMap); System.out.println("Traverse Using the keySet"); System.out.println("**************************"); traverseMapUsingKeySet(hashMap); System.out.println("Traverse Using the for-each"); System.out.println("**************************"); traverseMapUsingForEach(hashMap); System.out.println("Traverse Using the keySet iterator"); System.out.println("**************************"); traverseMapUsingKeySetIter(hashMap); System.out.println("Traverse Using stream"); System.out.println("**************************"); traverseMapUsingStreams(hashMap); } }
Output
Traverse Using the entrySet ************************** 1 = Hari Krishna 2 = Jaideep 3 = Sailaja 4 = Sankalp Traverse Using the keySet ************************** 1 = Hari Krishna 2 = Jaideep 3 = Sailaja 4 = Sankalp Traverse Using the for-each ************************** 1 = Hari Krishna 2 = Jaideep 3 = Sailaja 4 = Sankalp Traverse Using the keySet iterator ************************** 1 = Hari Krishna 2 = Jaideep 3 = Sailaja 4 = Sankalp Traverse Using stream ************************** 1=Hari Krishna 2=Jaideep 3=Sailaja 4=Sankalp
You may like
Remove entries from HashMap while iterating
No comments:
Post a Comment