Iterate through HashMap in Java

Read Time:6 Minute, 24 Second

Table of Contents

HashMap contains data in (Key, Value) pairs that can be accessed using a different index type. The Map interface is implemented by the HashMap class, which allows us to store keys. Since Java 1.2, hashMap has been a part of the Java collections framework. Internally, it employs a relatively quick hashing technique.

What is a HashMap in Java?

HashMap is a key-value pair storage container. Each key corresponds to a single value.

A HashMap’s keys must be unique. In other programming languages, HashMap is an associative array or a dictionary. HashMaps utilize more memory because each value has a corresponding key. The time it takes to delete and insert data is constant. You can store null values in HashMaps.

In HashMap, an Map.entry represents a key-value pair. HashMap’s entrySet method returns a set view of the map’s mappings. With the keySet method, you can get a list of keys.

Iterate through HashMap in Java examples

The syntax is as follows:


In this example, we’ll learn how to iterate over the keys, values, and key/value mappings of a Java HashMap. To follow along with this example, you should be familiar with the following Java programming concepts:

  • HashMap in Java
  • for-each loop in Java
  • Iterator Interface in Java

Different Traversal Methods

We can iterate over the key and value pairs that are listed below and detailed as follows:


  • Making Use of an Iterator
  • Looping with enhanced (for-each loop)
  • Using the method forEach()
  • Iterating through a HashMap with a for loop

Let’s explore what each of these methods does before we use them to iterate through a map:

  • entrySet() – produces a map collection-view whose items are drawn from the Map. In the entrySet() the key is returned by entry.getKey(), and the value is returned by entry.getValue().
  • keySet() – returns a list of all keys in this map.
  • values() – returns a list of all the values in this map.

Let’s have a look at how these strategies work in practice.


Using an iterator

Iterator is a java.util package interface that is used to iterate across a collection. As a result, there isn’t much to say about iterators, so we’ll propose some Iterator interface methods for traversing HashMap.

  • hm.entrySet() is used to retrieve all of the Map’s key-value pairs. Internally, a set is made up of entries and stores.
  • hm.entrySet().iterator() returns an iterator that functions as a cursor and advances from the set’s first to the last entry.
  • hmIterator.hasNext() returns a boolean if there is a next element in the set.
  • -The next element (Map.Entry) from the set is returned via
  • mapElement.getKey() is responsible for returning the key of the related Map.
  • mapElement.getValue() is responsible for returning the value of the related Map.

Example: Program for Traversing through the HashMap in Java

Example 1: Using Iterator

Example 2: Class for iterating HashMaps using an Iterator

Example 3: Iterate through HashMap using Iterator ()

In the example above, we’re iterating through the hash map’s keys, values, and key/value mappings. To iterate across the hashmap, we utilized the Iterator () method.


hasNext() returns true if the hashmap’s next element exists.


next() – returns the hashmap’s next entry.

Using for-each Loop

The forEach() method

HashMap’s forEach() method was first implemented in Java 8. It is used to iterate across the hashmap. At the same time, it is reducing the number of lines of code, as shown below:

Example 1: Program for traversing Through a HashMap in Java

Example 2: Class uses a forEach to iterate through a HashMap

Example 3: Iterate through HashMap using the forEach loop

We generated a hashmap named companies in the preceding example. Further, we used the forEach loop to iterate through the items of the hashmap in this case. It’s worth noting that we’re iterating through the keys, values, and key/value mappings separately.

  • companies.entrySet() – returns a list of all the entries in a set view.
  • companies.keySet() returns a set view of all critical companies.
  • values() – returns a list of all the values in a set view.

In this example, we used the Map.Entry class. The nested class returns a view of the map.

Iterating through a HashMap with a for loop

hash_map is used in the code below.


The function entrySet() returns a set view of the mapped components. It is now possible to iterate key-value pairs using the getValue() and getKey() operations.

Example: class for iterating HashMap in Java using a for loop

Using the Stream API and Lambdas

The Stream API and lambdas have been available in Java since version 8. Next, we’ll look at using these strategies to iterate a map.

Using forEach() and Lambda expressions

This, like so many other things in Java 8, turns out to be far less complicated than the alternatives. We’ll only use the forEach() method for now:

We don’t need to convert a map to a set of entries in this scenario. In fact, we can start here to learn more about lambda expressions. Of course, we may iterate through the map starting with the keys:


Similarly, we can utilize the values() method in the same way:

Using Stream API

One of Java 8’s most notable features is the Stream API. You can also use this feature to loop through a Map. A stream is a collection of elements from a source that can be aggregated in sequential and parallel ways. In addition, you can use a collection, IO operation, or array to source data for a stream.

When doing extra Stream processing, the Stream API should be used; otherwise, it’s just a basic forEach() as mentioned above. To demonstrate how the Stream API works, consider the following example:

The keySet() and values() methods of the Stream API are similar to the example above.

Example: Using Stream API for HashMap Iteration


The example iterates over a HashMap when using the stream API. The entrySet function returns the entry set, and the stream method returns the stream from the entry set. Later, we use forEach to iterate across the stream.

Iteration over an ArrayList-contained HashMap

Lists are also applicable as values in a HashMap. In this situation, we’ll require an extra loop.

We iterate through a HashMap with ArrayLists as values in this example. For loops, we utilize two.

As values, we define a HashMap using ArrayLists.

We go over the entering set with enhanced for loop. A key string and a list value are assigned to each entry.


The getKey method is used to obtain the key.

With getValue, we get the list.

We iterate over the list of values in the inner for loop.


In this article, we’ve looked at the numerous ways to iterate through the map’s items in Java. Simply said, we can use entrySet(), keySet(), or values() to extract the contents of a Map (). Because these are all sets, the iteration principles are the same for all of them. In addition, we also looked at Lambda expressions and the Stream API, which are only available in Java 8 and higher.


CyberSEO Pro - OpenAI GPT-3 autoblogging and content curation plugin for WordPress