map.keySet() / values() / entrySet()
These methods retrieve all keys, values, and entries of a map for iteration. They are used when you need to traverse all data in a map or combine it with the Stream API.
Syntax
// Returns all keys as a Set.
map.keySet();
// Returns all values as a Collection.
map.values();
// Returns all key-value pairs (entries) as a Set.
map.entrySet();
// Accesses a key-value pair.
Map.Entry<K, V> entry = ...;
entry.getKey();
entry.getValue();
// Performs an action on every entry (Java 8 and later).
map.forEach((key, value) -> { /* action */ });
Method List
| Method | Description |
|---|---|
| keySet() | Returns a Set containing all keys in the map. The set is backed by the map, so changes to the Set are reflected in the map. |
| values() | Returns a Collection containing all values in the map. Duplicate values are included. |
| entrySet() | Returns all key-value pairs as a Set<Map.Entry<K, V>>. This is the most efficient option when you need to work with both keys and values at the same time. |
| Map.Entry.getKey() | Returns the key of the entry. |
| Map.Entry.getValue() | Returns the value of the entry. |
| forEach(BiConsumer) | Performs an action on every entry using a lambda expression. Available in Java 8 and later. |
Sample Code
import java.util.HashMap;
import java.util.Map;
HashMap<String, Integer> scores = new HashMap<>();
scores.put("Alice", 85);
scores.put("Bob", 92);
scores.put("Carol", 78);
// Use keySet() to list all keys.
System.out.println(scores.keySet()); // Prints the Set of keys (order is not guaranteed).
for (String name : scores.keySet()) {
System.out.println(name + ": " + scores.get(name));
}
// Use values() to retrieve all values.
System.out.println(scores.values()); // Prints the Collection of values.
// Calculate the total score.
int total = 0;
for (int s : scores.values()) {
total += s;
}
System.out.println("Total: " + total); // Prints the total score.
// Use entrySet() to process keys and values at the same time (most efficient).
for (Map.Entry<String, Integer> entry : scores.entrySet()) {
System.out.println(entry.getKey() + " → " + entry.getValue() + " points");
}
// Use forEach() with a lambda for a more concise approach (Java 8 and later).
scores.forEach((name, score) -> {
System.out.println(name + ": " + (score >= 90 ? "Excellent" : "Pass"));
});
// Process entrySet() with the Stream API.
scores.entrySet().stream()
.filter(e -> e.getValue() >= 85)
.forEach(e -> System.out.println(e.getKey() + " is a top scorer."));
Notes
There are several ways to iterate over a map, but the recommended approach is to use entrySet(). Iterating with keySet() and calling get() on each key requires an extra lookup, whereas iterating over entries directly avoids that overhead.
In Java 8 and later, forEach() offers an even more concise syntax. The views returned by keySet() and entrySet() are backed by the original map, so adding or removing entries while iterating will throw a ConcurrentModificationException. To remove entries during iteration, use the remove() method of an Iterator.
To create, add, and retrieve map entries, see put() / get(). To remove entries and check for key existence, see remove() / containsKey().
If you find any errors or copyright issues, please contact us.