This write-up will talk about the approaches to iterate a “HashMap” in Java.
What’s a HashMap in Java?
A “HashMap” shops gadgets within the type of “key-value” pairs and these will be invoked by an index of some other kind (e.g., String). It additionally permits storing the null keys.
Find out how to Iterate a HashMap in Java?
A HashMap in Java will be iterated utilizing the below-given approaches:
Method 1: Iterate a HashMap in Java Utilizing “for” Loop
The “entrySet()” methodology provides a set view of all of the entries/components contained in a hashmap. The “getKey()” and “getValue()” strategies fetch the important thing and worth from the entry, respectively. These approaches will be utilized together to firstly entry the hashmap and iterate by means of the gathered keys and values utilizing the “for” loop.
Syntax
Within the above syntax, “hashmap” refers to a “HashMap” class object.
Instance
Let’s overview the below-provided instance:
import java.util.Map;
public class Instance {
public static void most important(String args[]) {
Map<Integer, String> custom_hashmap = new HashMap<Integer, String>();
custom_hashmap.put(1, “Harry”);
custom_hashmap.put(2, “David”);
custom_hashmap.put(3, “Sara”);
for (Map.Entry<Integer, String> set : custom_hashmap.entrySet()) {
System.out.println(set.getKey() + “: “ + set.getValue());
}}
}
Within the above-given code:
- Firstly, create a “HashMap” named “custom_hashmap” such that the secret’s specified as an “integer” and worth as “string” represented within the code as “Map<Integer, String>”.
- After that, affiliate the “put()” methodology with the hashmap to insert the acknowledged values within the type of “key-value” pairs.
- Now, apply the “for” loop mixed with the “entrySet()” methodology to iterate by means of the hashmap.
- Observe that the required “Map.Entry” interface permits the consumer to work with a map entry.
- Lastly, apply the “getKey()” and “getValue()” strategies to entry the allotted keys and values and show them.
Output
On this output, it may be analyzed that the set key-value pairs are iterated upon and displayed.
Method 2: Iterate a HashMap in Java Utilizing “forEach()” Methodology
The “forEach()” methodology is used to use a selected operation for every ingredient. This methodology will be applied to easily iterate by means of every key-value pair within the “HashMap” and show it.
Syntax
forEach(con<? tremendous E> x)
Within the above syntax, this methodology takes a parameter “x” which corresponds to the motion that must be carried out for every ingredient.
Instance
Let’s comply with the below-stated code:
import java.util.Map;
public class Instance {
public static void most important(String args[]) {
Map<Integer, String> custom_hashmap = new HashMap<Integer, String>();
custom_hashmap.put(1, “Harry”);
custom_hashmap.put(2, “David”);
custom_hashmap.put(3, “Sara”);
custom_hashmap.forEach((key,worth) -> System.out.println(key + “: “ + worth));
}}
Within the above illustration:
- Recall the mentioned approaches to create a “HashMap” and allocate the values within the type of “key-value” pairs.
- Now, affiliate the “forEach()” methodology with the created HashMap and show every of the iterated “key-value” pairs on the console.
Output
As you’ll be able to see that the keys and their corresponding values within the “HashMap” have been iterated.
Method 3: Iterate a HashMap in Java Utilizing “Iterator” Object
The “Iterator” object is used to loop by means of the weather one after the other, and the “iterator()” methodology will be utilized to fetch an Iterator. The “hasNext()” methodology provides “true” if there’s a subsequent ingredient contained within the hashmap, and the “subsequent()” methodology provides the following hashmap ingredient. These approaches will be utilized together to iterate by means of the HashMap, verify if there’s a subsequent key-value pair, and retrieve it.
Syntax
On this syntax:
- “x” is a set object.
- “iter” is of kind Iterator interface and corresponds to “x”.
Instance
Let’s overview the below-provided instance:
import java.util.Map.Entry;
import java.util.HashMap;
import java.util.Map;
public class Instance {
public static void most important(String args[]) {
Map<Integer, String> custom_hashmap = new HashMap<Integer, String>();
custom_hashmap.put(1, “Harry”);
custom_hashmap.put(2, “David”);
custom_hashmap.put(3, “Sara”);
Iterator<Entry<Integer, String>> iter = custom_hashmap.entrySet().iterator();
whereas (iter.hasNext()) {
Map.Entry<Integer, String> assign = (Map.Entry<Integer, String>) iter.subsequent();
System.out.println(assign.getKey() + “: “ + assign.getValue());
}}}
Within the above demonstration, apply the next steps:
- Repeat the mentioned methodologies for making a “HashMap” and allocating the “key-value” pairs.
- Now, affiliate the “Iterator” object with the created HashMap and loop by means of the key-value pairs with the assistance of the “entrySet()” and “iterator()” strategies.
- Lastly, look at the HashMap by checking the following ingredient through the utilized “hasNext()” methodology. In that case, show the following ingredient utilizing the “subsequent()” methodology.
- The connected “getKey()” and “getValue()” strategies will be sure that the gathered ingredient is fetched within the type of a “key-value” pair.
Output
The above output signifies that the iteration is finished appropriately.
Conclusion
A “HashMap” shops gadgets in “key-value” pairs. It may be iterated with the assistance of the “for” loop, the “forEach()” methodology, or the “Iterator” object. The iteration alongside a HashMap will be performed merely, by accessing every key-value pair, or by referring to the following ingredient, respectively. This weblog elaborated on the approaches to iterate a HashMap in Java.