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
- 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));
- }
- }
- }
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
- 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);
- }
- }
- }
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
- 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());
- }
- }
- }
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()); } } }