5 Best Ways to Iterate a Java HashMap: Code Examples & Tips

This article demonstrates five practical techniques for traversing a Java HashMap—including Iterator over EntrySet and KeySet, enhanced for‑each loops, lambda expressions, and the Stream API—each accompanied by complete, ready‑to‑run code samples.

ITFLY8 Architecture Home
ITFLY8 Architecture Home
ITFLY8 Architecture Home
5 Best Ways to Iterate a Java HashMap: Code Examples & Tips

1. Iterate HashMap using EntrySet with Iterator

Use an Iterator on the entrySet() to access both keys and values.

Map<Integer, String> coursesMap = new HashMap<>();
coursesMap.put(1, "C");
coursesMap.put(2, "C++");
coursesMap.put(3, "Java");
coursesMap.put(4, "Spring Framework");
coursesMap.put(5, "Hibernate ORM framework");

Iterator<Map.Entry<Integer, String>> iterator = coursesMap.entrySet().iterator();
while (iterator.hasNext()) {
    Map.Entry<Integer, String> entry = iterator.next();
    System.out.println(entry.getKey());
    System.out.println(entry.getValue());
}

2. Iterate HashMap using KeySet with Iterator

Iterate over the keys and retrieve values via get().

Map<Integer, String> coursesMap = new HashMap<>();
// (populate map as above)

Iterator<Integer> iterator = coursesMap.keySet().iterator();
while (iterator.hasNext()) {
    Integer key = iterator.next();
    System.out.println(key);
    System.out.println(coursesMap.get(key));
}

3. Iterate HashMap with Enhanced For‑Each Loop

The enhanced for loop simplifies iteration over entrySet().

for (Map.Entry<Integer, String> entry : coursesMap.entrySet()) {
    System.out.println(entry.getKey());
    System.out.println(entry.getValue());
}

4. Iterate HashMap using Lambda Expression

Leverage Map.forEach with a lambda to process each key‑value pair.

coursesMap.forEach((key, value) -> {
    System.out.println(key);
    System.out.println(value);
});

5. Iterate HashMap with Stream API

Use streams to traverse entries in a functional style.

coursesMap.entrySet().stream().forEach(entry -> {
    System.out.println(entry.getKey());
    System.out.println(entry.getValue());
});

Each method prints the key followed by its corresponding value, illustrating the most common patterns for HashMap traversal in Java.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaLambdaStreamIteratoriterationforeach
ITFLY8 Architecture Home
Written by

ITFLY8 Architecture Home

ITFLY8 Architecture Home - focused on architecture knowledge sharing and exchange, covering project management and product design. Includes large-scale distributed website architecture (high performance, high availability, caching, message queues...), design patterns, architecture patterns, big data, project management (SCRUM, PMP, Prince2), product design, and more.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.