Map<String, String> map = ...
for (Map.Entry<String, String> entry : map.entrySet()) {
    System.out.println(entry.getKey() + "/" + entry.getValue());
}

On Java 10+:

for (var entry : map.entrySet()) {
    System.out.println(entry.getKey() + "/" + entry.getValue());
}
Answer from ScArcher2 on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Map.Entry.html
Map.Entry (Java Platform SE 8 )
3 weeks ago - Java™ Platform Standard Ed. 8 ... A map entry (key-value pair). The Map.entrySet method returns a collection-view of the map, whose elements are of this class. The only way to obtain a reference to a map entry is from the iterator of this collection-view.
🌐
GeeksforGeeks
geeksforgeeks.org › java › hashmap-entryset-method-in-java
HashMap entrySet() Method in Java - GeeksforGeeks
August 6, 2025 - Return Value: The method returns a set having same elements as the hash map. Example 2: Here, we will use the entrySet() method with a HashMap having String keys and Integer values. ... // Using entrySet() method with a HashMap // having String keys and Integer values import java.util.*; public class GFG { public static void main(String[] args) { // Creating an empty HashMap HashMap<String, Integer> hm = new HashMap<>(); // Mapping integer values to string keys hm.put("Geeks", 10); hm.put("for", 15); hm.put("Geeks", 20); // Overwriting the value for key "Geeks" hm.put("Welcomes", 25); hm.put("You", 30); // Displaying the HashMap System.out.println("Initial Mappings: " + hm); // Using entrySet() to get the set view System.out.println("The set is: " + hm.entrySet()); } }
Top answer
1 of 16
5965
Map<String, String> map = ...
for (Map.Entry<String, String> entry : map.entrySet()) {
    System.out.println(entry.getKey() + "/" + entry.getValue());
}

On Java 10+:

for (var entry : map.entrySet()) {
    System.out.println(entry.getKey() + "/" + entry.getValue());
}
2 of 16
1633

To summarize the other answers and combine them with what I know, I found 10 main ways to do this (see below). Also, I wrote some performance tests (see results below). For example, if we want to find the sum of all of the keys and values of a map, we can write:

  1. Using iterator and Map.Entry

    long i = 0;
    Iterator<Map.Entry<Integer, Integer>> it = map.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<Integer, Integer> pair = it.next();
        i += pair.getKey() + pair.getValue();
    }
    
  2. Using foreach and Map.Entry

    long i = 0;
    for (Map.Entry<Integer, Integer> pair : map.entrySet()) {
        i += pair.getKey() + pair.getValue();
    }
    
  3. Using forEach from Java 8

    final long[] i = {0};
    map.forEach((k, v) -> i[0] += k + v);
    
  4. Using keySet and foreach

    long i = 0;
    for (Integer key : map.keySet()) {
        i += key + map.get(key);
    }
    
  5. Using keySet and iterator

    long i = 0;
    Iterator<Integer> itr2 = map.keySet().iterator();
    while (itr2.hasNext()) {
        Integer key = itr2.next();
        i += key + map.get(key);
    }
    
  6. Using for and Map.Entry

    long i = 0;
    for (Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator(); entries.hasNext(); ) {
        Map.Entry<Integer, Integer> entry = entries.next();
        i += entry.getKey() + entry.getValue();
    }
    
  7. Using the Java 8 Stream API

    final long[] i = {0};
    map.entrySet().stream().forEach(e -> i[0] += e.getKey() + e.getValue());
    
  8. Using the Java 8 Stream API parallel

    final long[] i = {0};
    map.entrySet().stream().parallel().forEach(e -> i[0] += e.getKey() + e.getValue());
    
  9. Using IterableMap of Apache Collections

    long i = 0;
    MapIterator<Integer, Integer> it = iterableMap.mapIterator();
    while (it.hasNext()) {
        i += it.next() + it.getValue();
    }
    
  10. Using MutableMap of Eclipse (CS) collections

    final long[] i = {0};
    mutableMap.forEachKeyValue((key, value) -> {
        i[0] += key + value;
    });
    

Perfomance tests (mode = AverageTime, system = Windows 8.1 64-bit, Intel i7-4790 3.60 GHz, 16 GB)

  1. For a small map (100 elements), score 0.308 is the best

    Benchmark                          Mode  Cnt  Score    Error  Units
    test3_UsingForEachAndJava8         avgt  10   0.308 ±  0.021  µs/op
    test10_UsingEclipseMap             avgt  10   0.309 ±  0.009  µs/op
    test1_UsingWhileAndMapEntry        avgt  10   0.380 ±  0.014  µs/op
    test6_UsingForAndIterator          avgt  10   0.387 ±  0.016  µs/op
    test2_UsingForEachAndMapEntry      avgt  10   0.391 ±  0.023  µs/op
    test7_UsingJava8StreamApi          avgt  10   0.510 ±  0.014  µs/op
    test9_UsingApacheIterableMap       avgt  10   0.524 ±  0.008  µs/op
    test4_UsingKeySetAndForEach        avgt  10   0.816 ±  0.026  µs/op
    test5_UsingKeySetAndIterator       avgt  10   0.863 ±  0.025  µs/op
    test8_UsingJava8StreamApiParallel  avgt  10   5.552 ±  0.185  µs/op
    
  2. For a map with 10000 elements, score 37.606 is the best

    Benchmark                           Mode   Cnt  Score      Error   Units
    test10_UsingEclipseMap              avgt   10    37.606 ±   0.790  µs/op
    test3_UsingForEachAndJava8          avgt   10    50.368 ±   0.887  µs/op
    test6_UsingForAndIterator           avgt   10    50.332 ±   0.507  µs/op
    test2_UsingForEachAndMapEntry       avgt   10    51.406 ±   1.032  µs/op
    test1_UsingWhileAndMapEntry         avgt   10    52.538 ±   2.431  µs/op
    test7_UsingJava8StreamApi           avgt   10    54.464 ±   0.712  µs/op
    test4_UsingKeySetAndForEach         avgt   10    79.016 ±  25.345  µs/op
    test5_UsingKeySetAndIterator        avgt   10    91.105 ±  10.220  µs/op
    test8_UsingJava8StreamApiParallel   avgt   10   112.511 ±   0.365  µs/op
    test9_UsingApacheIterableMap        avgt   10   125.714 ±   1.935  µs/op
    
  3. For a map with 100000 elements, score 1184.767 is the best

    Benchmark                          Mode   Cnt  Score        Error    Units
    test1_UsingWhileAndMapEntry        avgt   10   1184.767 ±   332.968  µs/op
    test10_UsingEclipseMap             avgt   10   1191.735 ±   304.273  µs/op
    test2_UsingForEachAndMapEntry      avgt   10   1205.815 ±   366.043  µs/op
    test6_UsingForAndIterator          avgt   10   1206.873 ±   367.272  µs/op
    test8_UsingJava8StreamApiParallel  avgt   10   1485.895 ±   233.143  µs/op
    test5_UsingKeySetAndIterator       avgt   10   1540.281 ±   357.497  µs/op
    test4_UsingKeySetAndForEach        avgt   10   1593.342 ±   294.417  µs/op
    test3_UsingForEachAndJava8         avgt   10   1666.296 ±   126.443  µs/op
    test7_UsingJava8StreamApi          avgt   10   1706.676 ±   436.867  µs/op
    test9_UsingApacheIterableMap       avgt   10   3289.866 ±  1445.564  µs/op
    

Graphs (performance tests depending on map size)

Table (perfomance tests depending on map size)

          100     600      1100     1600     2100
test10    0.333    1.631    2.752    5.937    8.024
test3     0.309    1.971    4.147    8.147   10.473
test6     0.372    2.190    4.470    8.322   10.531
test1     0.405    2.237    4.616    8.645   10.707
test2     0.376    2.267    4.809    8.403   10.910
test7     0.473    2.448    5.668    9.790   12.125
test9     0.565    2.830    5.952   13.220   16.965
test4     0.808    5.012    8.813   13.939   17.407
test5     0.810    5.104    8.533   14.064   17.422
test8     5.173   12.499   17.351   24.671   30.403

All tests are on GitHub.

🌐
Scaler
scaler.com › home › topics › java map entry
Java Map Entry - Scaler Topics
May 4, 2023 - There is now indexing for an entry in a map, so there is no direct way to access all the entries except the map.entrySet() method, which returns a collection-like view of a map whose elements are of type Map.Entry.
🌐
Baeldung
baeldung.com › home › java › java collections › java map › a guide to java hashmap
A Guide to Java HashMap | Baeldung
January 16, 2025 - Prior to Java 8: for(Map.Entry<String, Product> entry : productsByName.entrySet()) { Product product = entry.getValue(); String key = entry.getKey(); //do something with the key and value } Our article Guide to the Java 8 forEach covers the forEach loop in greater detail.
Find elsewhere
🌐
Medium
medium.com › @AlexanderObregon › javas-map-entryset-method-explained-1526a8cd58fb
Java’s Map.entrySet() Method Explained | Medium
December 15, 2024 - The Map.entrySet() method is a utility in Java’s java.util.Map class that provides a Set view of the map’s key-value pairs. This feature enables developers to efficiently access and manipulate entries, particularly in scenarios requiring iteration over both keys and values.
🌐
Java Journey
java-journey.com › 2025 › 11 › 03 › java-collections-framework-efficient-data-management-in-java
Java Collections Framework: Efficient Data Management in Java – Java Journey
November 3, 2025 - import java.util.*; public class MapExample { public static void main(String[] args) { Map<Integer, String> students = new HashMap<>(); students.put(1, "John"); students.put(2, "Alice"); students.put(3, "Bob"); for (Map.Entry<Integer, String> entry : students.entrySet()) { System.out.println("ID: " + entry.getKey() + ", Name: " + entry.getValue()); } } }
🌐
Oracle
docs.oracle.com › en › java › javase › 23 › docs › › api › java.base › java › util › Map.Entry.html
Map.Entry (Java SE 23 & JDK 23)
October 17, 2024 - var entries = map.entrySet().stream().map(Map.Entry::copyOf).toList() Since: 1.2 · See Also: Map.entrySet() All MethodsStatic MethodsInstance MethodsAbstract Methods · Modifier and Type · Method · Description · static <K extends Comparable<? super K>, V> Comparator ·
🌐
Baeldung
baeldung.com › home › java › java collections › java map › using the map.entry java class
Using the Map.Entry Java Class | Baeldung
August 1, 2024 - for (Map.Entry<String, String> book: bookMap.entrySet()) { System.out.println("key: " + book.getKey() + " value: " + book.getValue()); } In this example, our loop is over a collection of Map.Entry objects. As Map.Entry stores both the key and value together in one class, we get them both in a single operation. The same rules apply to using Java 8 stream operations.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Map.html
Map (Java Platform SE 8 )
3 weeks ago - for (Map.Entry<K, V> entry : map.entrySet()) entry.setValue(function.apply(entry.getKey(), entry.getValue()));
🌐
Programiz
programiz.com › java-programming › library › hashmap › entryset
Java HashMap entrySet()
The entrySet() method can be used with the for-each loop to iterate through each entry of the hashmap. import java.util.HashMap; import java.util.Map.Entry; class Main { public static void main(String[] args) { // Creating a HashMap HashMap<String, Integer> numbers = new HashMap<>(); ...
🌐
W3Schools
w3schools.com › java › ref_hashmap_entryset.asp
Java HashMap entrySet() Method
The entrySet() method returns a set containing all of the entries in the map. To learn about sets, see our Java HashSet tutorial. Note: The returned set is a view of the map, which means that changing the set also changes the map. public Set< ...
🌐
DevQA
devqa.io › 4-different-ways-iterate-map-java
Different Ways to Iterate Through a Map in Java
As of Java 8, we can use the forEach method as well as the iterator class to loop over a map. Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> entry : map.entrySet()) { System.out.println("Key = " ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › map-entry-interface-java-example
Map.Entry interface in Java with example - GeeksforGeeks
January 22, 2026 - Java C++ Python · Explanation: entrySet() retrieves all key–value pairs from the map. The enhanced for loop iterates over each Map.Entry. getKey() extracts the key from the current entry. getValue() returns the value associated with the current ...
🌐
Scaler
scaler.com › home › topics › hashmap entryset() method in java
HashMap entrySet() Method in Java - Scaler Topics
April 27, 2024 - HashMaps are used to store key-value pairs in Java, where all keys must be unique. The method returns a Set of maps.Entry objects represent a key-value pair contained in the HashMap. It enables direct access to both keys and values within the map.
🌐
Vultr
docs.vultr.com › java › standard-library › java › util › HashMap › entrySet
Java HashMap entrySet() - Retrieve Entry Set | Vultr Docs
November 19, 2024 - The entrySet() method in Java's HashMap is an essential tool for accessing the key-value pairs within the map. This method returns a set view of the mappings contained in the map, enabling you to interact with its contents via set operations ...