Web / Apache Commons Collections Interview questions
What is a MultiValuedMap in Apache Commons Collections?
A MultiValuedMap<K,V> lets a single key be associated with more than one value, storing the values for each key in a backing Collection instead of a single slot.
MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>(); map.put("fruits", "apple"); map.put("fruits", "banana"); Collection<String> values = map.get("fruits"); // [apple, banana]
Implementations differ in how they store the values per key: ArrayListValuedHashMap keeps them in an ArrayList (ordered, duplicates allowed), while HashSetValuedHashMap keeps them in a HashSet (unordered, duplicates collapsed).
Unlike a plain Map<K, List<V>> you build yourself, MultiValuedMap handles the null-checking and list creation for the first insert automatically.
More Related questions...