Skip to main content

Iterate HashMap in Java

How to Iterate hashmap in Java

In this article, we are going to see different ways to iterate hashmap in java. While using hashmap, we must know how to iterate hashmap in java as it is very common instance and we found ourselves again and again that we need to iterate hashmap. In this post, we will also see how java 8 makes the process of iteration over the hashmap is very easy.

Below, we are creating a HashMap and use this hashmap throughout this post to see the different ways of iteration of hashmap in java:
// Map to store Roll no. of students with their name
Map<Integer, String> hashMap = new HashMap<>();
hashMap.put(101, "Shubham");
hashMap.put(102, "Jatin");
hashMap.put(103, "Nikhil");
hashMap.put(104, "Priyanka");
hashMap.put(105, "Kajal");

How to iterate HashMap in Java using forEach() method

In this section, we will see the forEach() method to iterate hashmap in java, java provides forEach() method which takes an argument of BiConsumer, a BiConsumer is a consumer which takes 2 arguments and consume them.
(key, value) -> System.out.println(key + ":" + value)
When we invoked the forEach() method onto a map, then the BiConsumer provided as the argument and will be called and execute for each and every key-value pair stored in the hashmap. For example:
hashMap.forEach(
    (key, value) -> System.out.println(key + ":" + value)
);

Collections within HashMap in Java

To iterate hashmap in java, we can also fetch the Collection view of the hashmap. Hashmap provides 3 collections (which are just reflection of the hashmap) namely Entry Set, Key Set and Value Set. Let see how to iterate hashmap in java using each of the collection views one-by-one.

How to Iterate HashMap in Java using Entry Set

To iterate hashmap in java using Entry Set, we first need to understand Map.Entry<K,V> interface of java.util package.
An instance of this interface shows a key-value pair stored in a hashmap. The entrySet() method of hashmap, provides us a set of all entries or key-value pairs stored in the hashmap which we called EntrySet and this EntrySet can be iterated as a collection.
Set<Entry<Integer, String>> entrySet = hashMap.entrySet();
Using forEach() method :
entrySet.forEach(
     entry -> System.out.println(entry.getKey() + ":" + entry.getValue())
);
Using for-Each loop :
for (Entry<Integer, String> entry : entrySet) {
     System.out.println(entry.getKey() + ":" + entry.getValue());
}
Using iterators :
Iterator<Entry<Integer, String>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
     Entry<Integer, String> entry = iterator.next();
     System.out.println(entry.getKey() + ":" + entry.getValue());
}

How to Iterate HashMap in Java using Key Set

In this section, we see the approach of iterate hashmap in java using Key Set. The keySet() method returns the set of all keys stored in the hashmap known as KeySet. Then we can use keyset to iterate over the hashmap. Let see how
Set<Integer> keySet = hashMap.keySet();
Using forEach() method :
keySet.forEach(
     key -> System.out.println(key +":"+hashMap.get(key))
);
Using for-Each loop :
for(Integer key : keySet) {
     System.out.println(key +":"+hashMap.get(key));   
}
Using iterators :
Iterator<Integer> iterator = keySet.iterator();
while(iterator.hasNext()) {
     Integer key = iterator.next();
     System.out.println(key +":"+hashMap.get(key));
}

How to Iterate HashMap in Java using Value Set

In this section, we see the approach of iterate hashmap in java using Value Set, this approach can be used where we only need to use the values of the hashmap not the entire map with keys and values. The values() method is used to fetch the Value Set i.e, the list of all values stored in the hashmap.
Collection<String> values = hashMap.values();
Using forEach() method :
values.forEach(value -> System.out.println(value));
// Or By Method Reference
values.forEach(System.out::println);
Using for-Each loop :
for (String value : values) {
     System.out.println(value);
}
Using iterators :
Iterator<String> iterator = values.iterator();
while (iterator.hasNext()) {
     String value = iterator.next();
     System.out.println(value);
}

Conclusion

In this article, we have studied different approaches of how to iterate hashmap in java. Ofcourse, there are also exists another methods for iteration of hashmap as well, but these are the one that are easy to use and comes in usual coding practices.
Thanks for reading this article.
If, we missed something please let us know in the comment section below.
Happy Learning

Related Article

Comments