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
🌐
CSDN
cnblogs.com › keyi › p › 5821668.html
java Map及Map.Entry详解 - 路修远而求索 - 博客园
Map是java中的接口,Map.Entry是Map的一个内部接口。 Map提供了一些常用方法,如keySet()、entrySet(),values()等方法。 keySet()方法返回值是Map中key值的集合;entrySet()的返回值也是返回一个Set集合,此集合的类型为Map.Entry
🌐
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 › iterate-map-java
How to Iterate Any Map in Java? - GeeksforGeeks
July 23, 2025 - Map.entrySet() method returns a collection-view(Set<Map.Entry<K, V>>) of the mappings contained in this map. So, we can iterate over key-value pairs using getKey() and getValue() methods of Map.Entry<K, V>. This method is most common and should ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › hashmap-entryset-method-in-java
HashMap entrySet() Method in Java - GeeksforGeeks
August 6, 2025 - The entrySet() method of the HashMap class in Java is used to create a set view of the mappings contained in the HashMap.
🌐
Yawin
yawin.in › 10-efficient-ways-to-iterate-over-each-entry-in-a-java-map
Efficiently iterate over each entry in a Java Map – Yawin
You can also use a for-each loop to iterate over the entries of a Java Map. Here’s an example: Map<Integer, String> map = new HashMap<>(); map.put(1, "one"); map.put(2, "two"); map.put(3, "three"); for (Map.Entry<Integer, String> entry : map.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); }
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.

Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java collections › java map › how to create a new entry in a map
How to Create a New Entry in a Map | Baeldung
January 8, 2024 - Learn how to use Java's built-in classes, third-party libraries, and a custom implementation to create an Entry object in a Map.
🌐
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 15, 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.
🌐
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
January 8, 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 › class-use › Map.Entry.html
Uses of Interface java.util.Map.Entry (Java Platform SE 8 )
3 weeks ago - Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
🌐
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()));
🌐
Oracle
docs.oracle.com › javase › jp › 6 › api › java › util › Map.Entry.html
Map.Entry (Java Platform SE 6)
マップのエントリ (キーと値のペア) です。Map.entrySet メソッドは、このクラスに属する要素を持つマップのコレクションビューを返します。マップエントリへの参照を取得する唯一の方法は、このコレクションビューの...
🌐
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<>(); ...