HashMap with Looping

We can iterate hashmap using enhanced for loop with keyset() method or by using enhanced for loop with EntrySet() method or by using iterator

Case 1 :

Enhanced for loop with keyset()

Example

  1. import java.util.*;
  2. public class HashMapEnhancedForLoopExample {
  3. public static void main(String args[]) {
  4.  
  5. HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
  6.       hashMap.put(1, "One");
  7.       hashMap.put(2, "Two");
  8.       hashMap.put(3, "Three");
  9. Set<Integer> keys = hashMap.keySet();
  10.         for(Integer key: keys){
  11.             System.out.println("Value of "+key+" is: "+hashMap.get(key));
  12.         }
  13. }
  14. }
import java.util.*;
public class HashMapEnhancedForLoopExample {
public static void main(String args[]) {

HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
      hashMap.put(1, "One");
      hashMap.put(2, "Two");
      hashMap.put(3, "Three");
Set<Integer> keys = hashMap.keySet();
        for(Integer key: keys){
            System.out.println("Value of "+key+" is: "+hashMap.get(key));
        }
}
}


In this program, we have accessed all the keys of HashMap using keySet() method and then iterated all the keys using enhanced for loop and accessed value by passing key in each iteration in the loop.

Case 2 :

Enhanced for loop with entrySet()

Example

  1. import java.util.*;
  2. public class HashMapEnhancedForLoopEntrySetExample {
  3. public static void main(String args[]) {
  4.  
  5. HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
  6.       hashMap.put(1, "One");
  7.       hashMap.put(2, "Two");
  8.       hashMap.put(3, "Three");
  9. for (Map.Entry<Integer, String> entry : hashMap.entrySet()) {
  10.     Integer key = entry.getKey();
  11.     Object value = entry.getValue();
  12. System.out.println("Value of "+key+" is: "+value);
  13. }
  14.        
  15. }
  16. }
import java.util.*;
public class HashMapEnhancedForLoopEntrySetExample {
public static void main(String args[]) {

HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
      hashMap.put(1, "One");
      hashMap.put(2, "Two");
      hashMap.put(3, "Three");
for (Map.Entry<Integer, String> entry : hashMap.entrySet()) {
    Integer key = entry.getKey();
    Object value = entry.getValue();
System.out.println("Value of "+key+" is: "+value);
}
        
}
}


Case 3 :

Using iterator

Example

  1. import java.util.*;
  2. public class HashMapIteratorExample {
  3. public static void main(String args[]) {
  4.  
  5. HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
  6.       hashMap.put(1, "One");
  7.       hashMap.put(2, "Two");
  8.       hashMap.put(3, "Three");
  9. Iterator it = hashMap.entrySet().iterator();
  10.     while (it.hasNext()) {
  11.         Map.Entry pair = (Map.Entry)it.next();
  12.         System.out.println(pair.getKey() + " = " + pair.getValue());
  13.        
  14.     }
  15.        
  16. }
  17. }
import java.util.*;
public class HashMapIteratorExample {
public static void main(String args[]) {

HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
      hashMap.put(1, "One");
      hashMap.put(2, "Two");
      hashMap.put(3, "Three");
Iterator it = hashMap.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
        System.out.println(pair.getKey() + " = " + pair.getValue());
        
    }
        
}
}

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on