Web / Apache Commons Collections Interview questions
What happens when you add a duplicate value to a BidiMap?
A BidiMap enforces that every value maps back to exactly one key, so inserting a value that's already associated with a different key doesn't create a second mapping - it silently removes the old mapping first.
BidiMap<String, Integer> scores = new DualHashBidiMap<>(); scores.put("alice", 100); scores.put("bob", 100); // "alice" -> 100 is now gone System.out.println(scores.get("alice")); // null System.out.println(scores.getKey(100)); // "bob"
This is a deliberate consequence of keeping getKey(value) unambiguous: if two keys mapped to the same value, a reverse lookup wouldn't know which key to return, so the implementation resolves the conflict by evicting the previous owner of that value at put() time.
This is a common source of subtle bugs when migrating code from a regular Map to a BidiMap, since existing code that assumed duplicate values were harmless can silently lose entries after the switch.
More Related questions...