How to iterate or loop a HashMap?
This is one of the favourite question of Java interviewer's and already asked so many time in different forms with freshers as well as with experienced developers in interviews.Transversing a HashMap is little different from other collection framework classes.In this post, we are going to discuss 6 different ways to iterate or loop through HashMap.
We have taken a simple HashMap and looping that.
package com.javainstance;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* @author javainstance
*
*/
public class IterateHashmap {
public static void main(String[] args) {
//HashMap Creation
HashMap<Integer, String> hashmap = new HashMap<>();
hashmap.put(1, "Jai");
hashmap.put(2, "Abhay");
hashmap.put(3, "Prakash");
hashmap.put(4, "Bhanu");
hashmap.put(5, "Henery");
//Using Iterator and EntrySet
Set<Map.Entry<Integer, String>> entrySet = hashmap.entrySet();
Iterator<Entry<Integer, String>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
Entry<Integer, String> entry = iterator.next();
System.out.println("Key " + entry.getKey() + " " + "Value " + entry.getValue());
}
//Using keySet
Set<Integer> keys = hashmap.keySet();
for (Integer key : keys) {
System.out.println("Key " + key + " " + "Value " + hashmap.get(key));
}
//Using Iterator and keySet
Set<Integer> keyvalues = hashmap.keySet();
Iterator<Integer> iteratr = keyvalues.iterator();
while (iteratr.hasNext()) {
int key = iteratr.next();
System.out.println("Key " + key + " " + "Value " + hashmap.get(key));
}
//Using EntrySet and for loop
Set<Map.Entry<Integer, String>> entrySt = hashmap.entrySet();
for (Entry<Integer, String> ent : entrySt) {
System.out.println("Key " + ent.getKey() + " " + "Value " + ent.getValue());
}
// In JAVA 8 using forEach
hashmap.forEach((Integer, String) -> {
System.out.println("Key " + Integer + " " + "Value " + String);
}
);
// In JAVA 8 using forEach on entrySet
hashmap.entrySet().forEach(entry -> {
System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
});
}
}
Give your suggestion in comments.
If you like the post Share with your friends on social network.
Follow us: www.facebook.com/javainstance
You may be interested in Creating Java Web Application from Scratch- Java,Servlet, JSP, MySQL
We have taken a simple HashMap and looping that.
package com.javainstance;
import java.util.HashMap;import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* @author javainstance
*
*/
public class IterateHashmap {
public static void main(String[] args) {
//HashMap Creation
HashMap<Integer, String> hashmap = new HashMap<>();
hashmap.put(1, "Jai");
hashmap.put(2, "Abhay");
hashmap.put(3, "Prakash");
hashmap.put(4, "Bhanu");
hashmap.put(5, "Henery");
//Using Iterator and EntrySet
Set<Map.Entry<Integer, String>> entrySet = hashmap.entrySet();
Iterator<Entry<Integer, String>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
Entry<Integer, String> entry = iterator.next();
System.out.println("Key " + entry.getKey() + " " + "Value " + entry.getValue());
}
//Using keySet
Set<Integer> keys = hashmap.keySet();
for (Integer key : keys) {
System.out.println("Key " + key + " " + "Value " + hashmap.get(key));
}
//Using Iterator and keySet
Set<Integer> keyvalues = hashmap.keySet();
Iterator<Integer> iteratr = keyvalues.iterator();
while (iteratr.hasNext()) {
int key = iteratr.next();
System.out.println("Key " + key + " " + "Value " + hashmap.get(key));
}
//Using EntrySet and for loop
Set<Map.Entry<Integer, String>> entrySt = hashmap.entrySet();
for (Entry<Integer, String> ent : entrySt) {
System.out.println("Key " + ent.getKey() + " " + "Value " + ent.getValue());
}
// In JAVA 8 using forEach
hashmap.forEach((Integer, String) -> {
System.out.println("Key " + Integer + " " + "Value " + String);
}
);
// In JAVA 8 using forEach on entrySet
hashmap.entrySet().forEach(entry -> {
System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
});
}
}
Give your suggestion in comments.
If you like the post Share with your friends on social network.
Follow us: www.facebook.com/javainstance
You may be interested in Creating Java Web Application from Scratch- Java,Servlet, JSP, MySQL
How to iterate or loop a HashMap?
Reviewed by JavaInstance
on
11:41:00 AM
Rating:
Reviewed by JavaInstance
on
11:41:00 AM
Rating:
Nice explaination
ReplyDelete