In Java, replaceAll(function) visits every mapping of a HashMap and replaces its value with function.apply(key, oldValue). Keys are never added, removed, or changed.
The function is a BiFunction<K, V, V>, usually a lambda such as (item, price) -> price * 90 / 100. Because it receives the key too, each value can be computed differently depending on its key.
An instance method of java.util.HashMap (class since Java 1.2): a hash table of key/value pairs with no guaranteed order, one null key, any number of null values, and no thread safety. replaceAll(BiFunction) was added in Java 8 as a default method of Map; HashMap overrides it to update its entries in place.
Syntax
Pass a lambda that maps (key, old value) to the new value.
Sorted view of the result. Printing through a TreeMap lists cap, shirt, socks in alphabetical order.
Example 2 — Build Labels from Each Key and Value
Turn lowercase country names into labels that include the upper-case country code.
Java
import java.util.HashMap;
import java.util.Map;
public class HashMapReplaceAllLabels {
public static void main(String[] args) {
Map<String, String> countries = new HashMap<>();
countries.put("in", "india");
countries.put("jp", "japan");
countries.put("br", "brazil");
countries.replaceAll((code, name) ->
code.toUpperCase() + " - " + Character.toUpperCase(name.charAt(0)) + name.substring(1));
countries.forEach((code, label) -> System.out.println(code + " => " + label));
}
}
Output
br => BR - Brazil
in => IN - India
jp => JP - Japan
HashMap iteration order is not guaranteed. This is the typical output on OpenJDK 8–21; other JDKs or versions may print the entries in a different order.
How It Works
Key-dependent values. Each new value is built from both the key (the code) and the old value (the country name).
Capitalize.Character.toUpperCase(name.charAt(0)) raises the first letter and substring(1) appends the rest, so india becomes India.
Keys unchanged. The printed keys are still lowercase: replaceAll can change values only.
Alphabetical by luck. The forEach output follows the table order of the three two-letter keys; that it looks alphabetical here is a coincidence of their hash codes.
Example 3 — null Results Keep the Key; a null Function Throws
Disable large coupons by mapping them to null, then pass null as the function.
after: {SAVE10=10, SAVE50=null, SAVE5=5}
SAVE50 still a key? true
replaceAll(null) -> NullPointerException
HashMap iteration order is not guaranteed. This is the typical output on OpenJDK 8–21; other JDKs or versions may print the entries in a different order.
How It Works
Keep or blank. The lambda returns the same percentage for small coupons and null for anything above 20.
null is stored. SAVE50 now maps to null; unlike compute, replaceAll does not remove a key whose new value is null.
Still present.containsKey("SAVE50") is true, so remove the key separately if it should disappear.
Function required.replaceAll(null) throws NullPointerException instead of doing nothing.
Frequently Asked Questions
Call map.replaceAll((key, value) -> newValue). The lambda runs once per mapping and its result becomes the new value.
No. Only values change. To rename keys, build a new map.
In a HashMap the key stays and its value becomes null. Use entrySet().removeIf(...) to drop entries.