This examples shows you how to iterate through a HashMap in Java
Iterate over a collection or data set in Java is a very common task. You can use it to print or manipulate the data. Following examples show three different approaches on how to iterate over a HashMap. Depending on your Java version you can choose one of them.
Using for each to iterate through a HashMap
This is the advised approach. It gives you full control over the keys and values in the map. With this approach you actually iterate through the EntrySet
of the map and you get the key and value of each entry inside the loop.
For each is available in Java versions 1.5 +
import java.util.HashMap;
public class IterateHashMap {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
import java.util.HashMap;
import java.util.Map;
public class IterateHashMap {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
}
import java.util.HashMap;
import java.util.Map;
public class IterateHashMap {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
}
Iterate through HashMap with Lambda Expressions in Java 8
This approach is available in Java 8 + versions.
import java.util.HashMap;
public class IterateHashMap {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.forEach((key,value) -> System.out.println(key + " = " + value));
import java.util.HashMap;
import java.util.Map;
public class IterateHashMap {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.forEach((key,value) -> System.out.println(key + " = " + value));
}
}
import java.util.HashMap;
import java.util.Map;
public class IterateHashMap {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.forEach((key,value) -> System.out.println(key + " = " + value));
}
}
Iterate through HashMap using Iterator
This method uses java.util.Iterator
to go through the HashMap. This was the default approach in Java 1.4 and older versions. Although the following example uses Generics, so you need Java 1.5 + to execute it
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
public class IterateHashMap {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
Iterator<Entry<String, String>> it = map.entrySet().iterator();
Map.Entry<String, String> pair = (Map.Entry<String, String>) it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class IterateHashMap {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
Iterator<Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> pair = (Map.Entry<String, String>) it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
}
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class IterateHashMap {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
Iterator<Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> pair = (Map.Entry<String, String>) it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
}
}
}
4.2
5
votes
Article Rating
Post Views:
208,655
Related Tutorials
-
Choose the Right Java CollectionJava offers you a variety of collection implementations to choose from. In general you will always look for the collection with the best performance for your…
-
Basic Java Regular ExpressionsToday I will show you the very basics of Regular Expressions and how to use them in Java. This won't be a extensive and detailed…
-
Java Increase MemoryThe Java memory model specifies how the Java virtual machine works with the computer's memory (RAM). In this tutorial I will show you how to…
Hi it is the fundamental concept of java called Constructors of HashMap class. It is very basic concept to crack java interview for freshers.
HashMap() — Used to create a default HashMap.
HashMap(Map m) — Used to create a map using the the elements of give map object which is m in the parameter.
HashMap(int capacity) — Used to create the HashMap with the given capacity.
HashMap(int capacity, float fillRatio) — Used to create the HashMap with the given capacity along with the load factor which also called fillRatio in HashMap in java