HashMap is a part of the java collections framework. It internally uses hashing technique.
This post contains different ways of traversing through a HashMap, which are given below:
- Using an Iterator: Iterator is an interface in java.util package which is used to iterate through a collection.
- hm.entrySet() is used to retrieve all the key-value pairs called Map.Entries and stores internally into a set.
- hm.entrySet().iterator() returns a iterator which acts as a cursor and points at the first element of the set and moves on till the end.
- hmIterator.hasNext() checks for the next element in the set and returns a boolean
- hmIterator.next() returns the next element(Map.Entry) from the set.
- mapElement.getKey() returns the key of the associated Map.Entry
- mapElement.getValue() return the value of the associated Map.Entry
Example:
// Java program to traverse through// a hashmap using iteratorimportjava.util.*;classGfG {publicstaticvoidmain(String[] args){// Consider the hashmap contains// student name and their marksHashMap<String, Integer> hm =newHashMap<String, Integer>();// Adding mappings to HashMaphm.put("GeeksforGeeks",54);hm.put("A computer portal",80);hm.put("For geeks",82);// Printing the HashMapSystem.out.println("Created hashmap is"+ hm);// Getting an iteratorIterator hmIterator = hm.entrySet().iterator();// Iterate through the hashmap// and add some bonus marks for every studentSystem.out.println("HashMap after adding bonus marks:");while(hmIterator.hasNext()) {Map.Entry mapElement = (Map.Entry)hmIterator.next();intmarks = ((int)mapElement.getValue() +10);System.out.println(mapElement.getKey() +" : "+ marks);}}}chevron_rightfilter_noneOutput:Created hashmap is{GeeksforGeeks=54, A computer portal=80, For geeks=82} HashMap after adding bonus marks: GeeksforGeeks : 64 A computer portal : 90 For geeks : 92 - Using for-each loop:
// Java program for traversing// through a hashmap using for-each loopimportjava.util.*;classGfG {publicstaticvoidmain(String[] args){// Consider the hashmap containing// student name and their marksHashMap<String, Integer> hm =newHashMap<String, Integer>();// Adding mappings to HashMaphm.put("GeeksforGeeks",54);hm.put("A computer portal",80);hm.put("For geeks",82);// Printing the HashMapSystem.out.println("Created hashmap is"+ hm);// Loop through the hashmapSystem.out.println("HashMap after adding bonus marks:");// Using for-each loopfor(Map.Entry mapElement : hm.entrySet()) {String key = (String)mapElement.getKey();// Add some bonus marks// to all the students and print itintvalue = ((int)mapElement.getValue() +10);System.out.println(key +" : "+ value);}}}chevron_rightfilter_noneOutput:Created hashmap is{GeeksforGeeks=54, A computer portal=80, For geeks=82} HashMap after adding bonus marks: GeeksforGeeks : 64 A computer portal : 90 For geeks : 92 - Using forEach() method: forEach() is a method of hashmap which is introduced in java 8. It is used to iterate through the hashmap and also reduces the number of lines of code.
// Java program for traversing// through a hashmap using for-each loopimportjava.util.*;classGfG {publicstaticvoidmain(String[] args){// Consider the hashmap containing// student name and their marksHashMap<String, Integer> hm =newHashMap<String, Integer>();// Adding mappings to HashMaphm.put("GeeksforGeeks",54);hm.put("A computer portal",80);hm.put("For geeks",82);// Printing the HashMapSystem.out.println("Created hashmap is"+ hm);// Loop through the hashmap// and add bonus marksSystem.out.println("HashMap after adding bonus marks:");// Using Hashmap.forEach()hm.forEach((k, v) -> System.out.println(k +" : "+ (v +10)));}}chevron_rightfilter_noneOutput:Created hashmap is{GeeksforGeeks=54, A computer portal=80, For geeks=82} HashMap after adding bonus marks: GeeksforGeeks : 64 A computer portal : 90 For geeks : 92
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Traverse through a HashSet in Java
- Java Program to Traverse in a Directory
- How to traverse a C++ set in reverse direction
- HashMap and TreeMap in Java
- HashMap Class Methods in Java with Examples | Set 1 (put(), get(), isEmpty() and size())
- Hashmap methods in Java with Examples | Set 2 (keySet(), values(), containsKey()..)
- Hashmap vs WeakHashMap in Java
- Internal Working of HashMap in Java
- HashMap in Java with Examples
- Differences between HashMap and HashTable in Java
- Differences between TreeMap, HashMap and LinkedHashMap in Java
- Sorting a HashMap according to keys in Java
- Check whether two Strings are Anagram of each other using HashMap in Java
- HashMap clear() Method in Java
- HashMap containsKey() Method in Java
- HashMap clone() Method in Java
- HashMap containsValue() Method in Java
- HashMap isEmpty() Method in Java
- HashMap entrySet() Method in Java
- HashMap get() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

