Problem overview
The method invertDictionary receives a HashMap<String, String> named d and returns a new map with keys and values swapped. Every value of d becomes a key, and it maps to the key it belonged to.
With {"apple": "red", "banana": "yellow", "plum": "purple"} the result is {red=apple, yellow=banana, purple=plum}. With {"cat": "dog"} it is {dog=cat}. The lesson promises that all values of d are different, so the result has as many entries as d. The order of the entries doesn't matter.
A map can use the same string as a key and as a value. In {"a": "b", "b": "c"}, "b" is the value of "a" and also a key. Inverted, the entry a=b becomes b=a, and the entry b=c becomes c=b. The result is {b=a, c=b}. The two roles of "b" don't interfere, because the result is a separate, new map.
Approach: turn each entry around into a new map
Read one entry at a time and write its mirror image into a fresh map. A key and its value are two halves of one entry, so you need both while you work. Writing into a new map, instead of changing d while looping over it, keeps the loop safe: changing a map during iteration over its keys makes Java throw a ConcurrentModificationException.
Step by step
- Create the empty result.
HashMap<String, String> result = new HashMap<>();receives the swapped pairs. - Loop over the keys.
d.keySet()returns the keys, and the enhancedforloop visits each as aString key. - Read the value with
d.get(key)into aString value. The key exists, because it came from the map, sogetnever returnsnullhere. - Store the swapped pair:
result.put(value, key). The old value is the new key, and the old key is the new value. - Return
resultafter the loop.
The solution in Java
class Solution {
public HashMap<String, String> invertDictionary(HashMap<String, String> d) {
HashMap<String, String> result = new HashMap<>();
for (String key : d.keySet()) {
String value = d.get(key);
result.put(value, key);
}
return result;
}
}
Java details worth knowing:
keySet()and the order. AHashMapvisits its keys in an order of its own, so the loop can handle the entries in any order. It doesn't matter here, because each entry is independent.- A shorter loop.
for (Map.Entry<String, String> e : d.entrySet())gives the key and the value together withgetKey()andgetValue(), so nogetcall is needed. The lesson's version usesgetbecause it shows each step plainly. - Distinct values matter. If two keys shared a value, the second
putwould overwrite the first and one entry would be lost. With{"a": "x", "b": "x"}the inverse has a single entry, and which key survives depends on the iteration order. The lesson rules this out. - The original is untouched.
dis only read, so it keeps its entries after the call.
Complexity
Time: O(n) for n entries: each costs one get and one put, both constant on average. Space: O(n), since the new map has as many entries as d.
Edge cases and common mistakes
- Calling
put(key, value)by habit. That copies the map instead of inverting it. The value goes first. - Modifying
dinside the loop. Adding or removing keys of the map being iterated throws aConcurrentModificationException. - A value that is also a key. It needs no special handling, as the
{"a": "b", "b": "c"}example shows. - Duplicate values. They lose entries. If a lesson allowed them, the inverse would need a list as its value type.
Testing
{"apple": "red", "banana": "yellow", "plum": "purple"}gives{red=apple, yellow=banana, purple=plum}.{"cat": "dog"}gives{dog=cat}.{"a": "b", "b": "c"}gives{b=a, c=b}.
