Web / Apache Commons Collections Interview questions
What is the difference between the legacy MultiMap (3.x) and MultiValuedMap (4.x)?
In Commons Collections 3.x, MultiMap was defined by extending java.util.Map<K, Object>, where get(key) returned a raw Object that callers had to manually cast to a Collection - a design that predates generics being taken seriously and breaks the Map contract's expectation that get() returns a single, consistently-typed V.
| MultiMap (3.x) | MultiValuedMap (4.x) |
| extends Map<K, Object> | its own top-level interface, doesn't extend Map |
| get(key) returns Object, needs manual casting | get(key) returns Collection<V> directly, type-safe |
| no clean Map-shaped view available | asMap() provides a Map<K, Collection<V>> view when needed |
4.x resolved this by making MultiValuedMap<K,V> its own top-level interface rather than forcing it to extend Map, so get(key) can be properly declared to return Collection<V> without violating any inherited contract, while still offering an asMap() bridging method for code that specifically needs a Map-shaped view of the same data.
More Related questions...