Java HashMap in Depth
A HashMap stores pairs, one key with one value. This lesson walks a map by its entries and updates it without overwriting by accident.
Walking the entries in key order
entrySet hands over every pair in the map. The entries are sorted by key before printing, so the order is stable.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<>();
scores.put("Cid", 88);
scores.put("Ana", 92);
scores.put("Ben", 75);
List<Map.Entry<String, Integer>> entries = new ArrayList<>(scores.entrySet());
for (int i = 0; i < entries.size(); i++) {
for (int j = i + 1; j < entries.size(); j++) {
if (entries.get(i).getKey().compareTo(entries.get(j).getKey()) > 0) {
Map.Entry<String, Integer> temp = entries.get(i);
entries.set(i, entries.get(j));
entries.set(j, temp);
}
}
}
for (Map.Entry<String, Integer> entry : entries) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}A safe answer for a missing key
getOrDefault looks up a key and falls back to the given value. Ana is present, and Dan falls back to zero.
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<>();
scores.put("Ana", 92);
scores.put("Ben", 75);
System.out.println(scores.getOrDefault("Ana", 0));
System.out.println(scores.getOrDefault("Dan", 0));
}
}Writing only when absent
putIfAbsent tries to add Ana with a new score of 100. The stored score survives, and the new key Dan is added.
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<>();
scores.put("Ana", 92);
scores.putIfAbsent("Ana", 100);
scores.putIfAbsent("Dan", 80);
System.out.println(scores.get("Ana"));
System.out.println(scores.get("Dan"));
}
}Replacing a present value
replace updates Ben, who is in the map. Dan is not a key, so the second replace changes nothing.
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<>();
scores.put("Ana", 92);
scores.put("Ben", 75);
scores.replace("Ben", 88);
scores.replace("Dan", 50);
System.out.println(scores.get("Ben"));
System.out.println(scores.getOrDefault("Dan", 0));
}
}Example
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> grades = new HashMap<>();
grades.put("Ana", 92);
grades.put("Ben", 75);
grades.put("Cid", 88);
grades.putIfAbsent("Ben", 60);
grades.replace("Cid", 91);
List<String> keys = new ArrayList<>(grades.keySet());
Collections.sort(keys);
for (String key : keys) {
System.out.println(key + ": " + grades.get(key));
}
}
}Key points
- entrySet returns the pairs of the map, and each Map.Entry holds one key with its value.
- A printed listing sorts the entries by key first, because a HashMap keeps no order of its own.
- getOrDefault answers a chosen value when a key is missing, with no null check.
- putIfAbsent writes a pair only when the key has no value yet.
- replace changes the value of a key that is already present, and ignores a missing key.
In this example, putIfAbsent leaves the stored score for Ben untouched, replace raises the score for Cid, and the listing prints in key order.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does putIfAbsent do when the key already holds a value?