Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Simplify Multi-Value Mapping in Java with Apache Commons Collections MultiValuedMap

Simplify Multi-Value Mapping in Java with Apache Commons Collections MultiValuedMap

This tutorial demonstrates how Apache Commons Collections 4's MultiValuedMap interface eliminates boilerplate code when mapping keys to multiple values, covering core operations, three implementations (ArrayListValuedHashMap, HashSetValuedHashMap, UnmodifiableMultiValuedMap), and practical code examples with output.

Standard java.util.Map enforces a one-key-to-one-value relationship; inserting a duplicate key overwrites the previous value. Business scenarios often require one key to hold multiple values — such as a user's multiple orders, an API's multiple request parameters, or grouped detail records. The typical workaround is Map<K, List<V>>, which forces developers to write repetitive null-checks, list instantiation, and computeIfAbsent logic.

Solution: MultiValuedMap

Apache Commons Collections 4 provides the MultiValuedMap interface (common implementations: ArrayListValuedHashMap, HashSetValuedHashMap) that encapsulates multi-value semantics. A single put or putAll call replaces five to six lines of manual list handling.

Maven Dependency

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-collections4</artifactId>
  <version>4.5.0</version>
</dependency>

Basic Operations

Adding Data

MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
map.put("fruits", "apple");
map.put("fruits", "orange");
map.putAll("vehicles", List.of("car", "bike"));
System.err.println(map);
System.err.println(map.get("fruits"));

Output:

{fruits=[apple, orange], vehicles=[car, bike]}
[apple, orange]

Retrieving Data

get(key)

returns a Collection<V> of all values for the key. entries() returns a Collection<Map.Entry<K,V>> of every key-value pair (each value appears as a separate entry). keys() returns a MultiSet<K> showing each key with its multiplicity; keySet() returns a distinct Set<K>. values() returns a flat Collection<V> of all values across all keys.

Removing Data

remove(key)

deletes all values for the key and returns the removed collection. removeMapping(key, value) deletes a single key-value pair, leaving other values for the same key intact. clear() empties the entire map.

Checking Data

containsKey(key)

, containsValue(value), containsMapping(key, value) for existence checks. isEmpty() and size() for overall state.

Implementation Variants

ArrayListValuedHashMap

Backed by ArrayList per key; allows duplicate key-value pairs. Not thread-safe.

MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
map.put("fruits", "apple");
map.put("fruits", "orange");
map.put("fruits", "orange");
System.err.println(map); // {fruits=[apple, orange, orange]}

HashSetValuedHashMap

Backed by HashSet per key; duplicates are silently ignored. Not thread-safe.

MultiValuedMap<String, String> map = new HashSetValuedHashMap<>();
map.put("fruits", "apple");
map.put("fruits", "apple");
System.err.println(map); // {fruits=[apple]}

UnmodifiableMultiValuedMap

Decorator that wraps any MultiValuedMap to make it immutable; any mutating operation throws UnsupportedOperationException.

MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
map.put("fruits", "apple");
map.put("fruits", "orange");
MultiValuedMap<String, String> immutableMap = MultiMapUtils.unmodifiableMultiValuedMap(map);
immutableMap.put("fruits", "banana"); // throws exception
About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Published

AI-assisted summary

Report an issue with this article
JavaBackend DevelopmentData StructuresCode SimplificationCollections FrameworkApache Commons CollectionsMultiValuedMap

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion