Web / Apache Commons Collections Interview questions
What is the difference between a BidiMap and a regular Map?
A regular Map<K,V> only supports efficient forward lookup, from key to value; finding a key from a given value means manually iterating every entry, an O(n) operation, and duplicate values are perfectly allowed.
| Map<K,V> | BidiMap<K,V> |
| reverse lookup requires manual iteration | getKey(value) gives O(1)-average reverse lookup |
| duplicate values allowed | values must be unique |
| no inverse view | inverseBidiMap() exposes the reverse mapping as its own BidiMap |
A BidiMap keeps a second internal index built on the values so getKey(value) runs in constant time on average, but that convenience comes at the cost of the uniqueness constraint on values - inserting a duplicate value silently removes the mapping that previously held it.
More Related questions...