Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java HashMap Values: How to Retrieve and Iterate

Retrieve, iterate, and update values in a Java HashMap, with null handling, performance tradeoffs, and safe iteration patterns.

~5 min readJava
#HashMap#Java Collections#Iteration#Null Handling
Illustration of a Java HashMap showing key-value pairs and the values collection view.

The HashMap class is one of the most used collections in Java, and working with the values stored in a HashMap is a routine part of many applications. Whether you need to fetch a single value, traverse the entire collection, or update entries in place, the API offers several approaches with different tradeoffs. Understanding these options helps you write code that is both correct and efficient.

Consider a simple map that stores user IDs to names:

java
Map<Integer, String> users = new HashMap<>(); users.put(1, "Alice"); users.put(2, "Bob"); users.put(3, "Charlie");

This map will be used throughout the examples below.

Retrieving a Single Value with get() and getOrDefault()

The most direct way to obtain a value is the get method, which returns the value associated with a key, or null if the key is not present.

java
String name = users.get(2); // "Bob" String missing = users.get(99); // null

A common problem is that get returns null both when the key is absent and when the key maps to a null value. To avoid ambiguity, use containsKey when you need to distinguish these cases. For scenarios where a default value is acceptable, getOrDefault is cleaner:

java
String name = users.getOrDefault(99, "Unknown");

This avoids a separate null check and makes the fallback explicit. Note that getOrDefault does not add the key to the map; it simply returns the default if the key is missing.

Iterating Over All Values with values()

The values() method returns a Collection<V> view of the values contained in the map. This view is backed by the map, so changes to the map are reflected in the collection, and vice versa for operations that are supported.

java
Collection<String> allNames = users.values(); for (String name : allNames) { System.out.println(name); }

The iteration order is not guaranteed unless the map is a LinkedHashMap, which maintains insertion order by default. If you need a predictable order, consider using LinkedHashMap instead of HashMap.

The values() view does not support adding new elements, because there is no key to associate with a value. Attempting to call add on the returned collection throws UnsupportedOperationException. However, removing elements is allowed, either through Iterator.remove() or Collection.remove().

Modifying Values During Iteration

If you need to remove values while iterating, use the iterator's remove method to avoid ConcurrentModificationException:

java
Iterator<String> iterator = users.values().iterator(); while (iterator.hasNext()) { String name = iterator.next(); if (name.startsWith("A")) { iterator.remove(); } }

This removes the corresponding key-value pair from the map. Directly modifying the map (for example, calling users.remove(key)) inside a for-each loop will cause a ConcurrentModificationException because the iteration is fail-fast.

If you need to update the value objects themselves, you can retrieve each value and modify its state, provided the value type is mutable. For example, if the map holds List objects, you can add elements to each list without structural modification of the map.

Handling Null Values and Null Keys

HashMap allows both null keys and null values. A map can contain at most one null key, but many keys can map to null. When you call get with a key that maps to null, it returns null, which is indistinguishable from a missing key unless you use containsKey.

java
users.put(4, null); System.out.println(users.containsKey(4)); // true System.out.println(users.get(4)); // null

When iterating over values(), a null value appears as a normal element. If your logic assumes non-null values, you need to filter them out explicitly:

java
for (String name : users.values()) { if (name != null) { // process name } }

Alternatively, use Objects::nonNull as a filter in streams.

Performance and Memory Considerations

HashMap provides average constant-time performance for get, put, and containsKey under uniform hash distribution. Iterating over values() is linear in the number of entries, which is typical for any collection view. The values() view itself does not copy data; it is a lightweight object that delegates to the map's internal storage. Therefore, creating a values() view has negligible memory overhead.

One performance pitfall is repeatedly calling get inside a loop over keySet() when you need both keys and values. This doubles the lookup cost. Instead, iterate over entrySet() to retrieve key-value pairs in one pass.

Another consideration is that the iteration order of a HashMap can change when the map is resized or when entries are removed. If stable iteration order matters, use LinkedHashMap for insertion order or TreeMap for sorted order.

Choosing Between values(), keySet(), and entrySet()

Each view serves a different purpose:

ViewReturnsUse case
values()Collection<V>When only values matter
keySet()Set<K>When only keys matter or for key-based ops
entrySet()Set<Map.Entry<K,V>>When both key and value are needed

Use values() when you are aggregating or processing the values independently of their keys. For example, summing a map of numeric values or collecting all names into a list. Use entrySet() when the key is required for the operation, such as updating a value based on its key. keySet() is useful for tasks like checking existence or removing entries by key, but it is less efficient if you also need the values.

Using Streams with HashMap Values

The values() collection can be turned into a stream for functional-style processing:

java
List<String> longNames = users.values().stream() .filter(name -> name != null && name.length() > 3) .collect(Collectors.toList());

Streams allow concise transformations, but be aware that the stream is evaluated lazily and the underlying map must not be structurally modified during the stream operation unless you use a concurrent map. For parallel streams, consider the thread-safety of the map; a plain HashMap is not safe for concurrent modification.

When you need to aggregate values, reduce or collect are more expressive than manual loops. However, for simple iterations, a for-each loop is often more readable and has less overhead. Choose the approach that matches the complexity of the operation.

Handling Concurrent Modification and Thread Safety

If multiple threads access a HashMap and at least one thread modifies it structurally, the map must be synchronized externally. The values() view inherits this requirement. Using Collections.synchronizedMap wraps the map and its views, but iteration still requires manual synchronization:

java
Map<Integer, String> syncMap = Collections.synchronizedMap(users); synchronized (syncMap) { for (String value : syncMap.values()) { // safe iteration } }

For higher concurrency, ConcurrentHashMap is a better choice. Its values() view is weakly consistent: iterators do not throw ConcurrentModificationException, and they may or may not reflect modifications made after the iterator was created. This behavior is suitable for read-heavy workloads but requires careful reasoning about consistency.

When using ConcurrentHashMap, the values() view supports removal through its iterator and through collection methods such as removeIf. Bulk operations on the view are not atomic with respect to the whole map, so concurrent updates can be interleaved. Adding elements through the view is still unsupported.

A Practical Example: Aggregating Values

Consider a scenario where you have a map of product IDs to prices and you need to calculate the total cost of a subset of products. Using values() and a stream makes this straightforward:

java
Map<Integer, Double> prices = new HashMap<>(); prices.put(101, 19.99); prices.put(102, 29.99); prices.put(103, 9.99); double total = prices.values().stream() .filter(price -> price > 15.0) .mapToDouble(Double::doubleValue) .sum();

This approach avoids the need to know the keys, which is exactly the intent of the values() view. If you later need to report which products contributed to the total, you would switch to entrySet() to retain key information.

The choice between values(), keySet(), and entrySet() ultimately depends on whether the key is part of the operation. By selecting the appropriate view, you keep the code expressive and avoid unnecessary lookups or data copying.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.