org.apache.commons.collections – MultiValueMap

May 20, 2012 – 12:38 pm

Maps, Treemaps and whatever have you, are a great way to store data for easy access the data by a given key. The disadvantage with this objects is that the key has to be unique. This means that you cannot store different values associated with one key.

I needed a solution to store data in a map and each key should be used for multiple values

key = human, value = Batman
key = human, value = Captain America
key = alien, value = Superman

Searching the web, I found org.apache.commons.collections

A MultiMap is similar to a Map, but which may associate multiple values with a single key. If you call put(K, V) twice, with the same key but different values, the multimap contains mappings from the key to both values.

This makes it very easy to filter a map by a given key.

package de.eknori.test;
public class MultiMap {
	public static void main(String[] args) {
		MultiValueMap superheroes = new MultiValueMap();
		String key = "human";
		superheroes.put("mutant", "Wolverine");
		superheroes.put("mutant", "Beast");
		superheroes.put("alien", "Superman");
		superheroes.put("human", "Batman");
		superheroes.put("human", "Captain America");
		System.out.println(key + " superheroes: " + superheroes.get(key));
		System.out.println("There are " + superheroes.size()
				+ " different kind of superheroes: " + superheroes.keySet());
		System.out.println("All superheroes: " + superheroes.values());

Running the code will show the following on the console:

human superheroes: [Batman, Captain America]
There are 3 different kind of superheroes: [alien, mutant, human]
All superheroes: [Superman, Wolverine, Beast, Batman, Captain America]

  1. 3 Responses to “org.apache.commons.collections – MultiValueMap”

  2. Looks interesting but maybe I’m missing something. Can’t you put an object into a map? Tree or hash? As the value? So an array or custom object that could hold multiple values? I thought so at least. That could give you your multi values I’d think.
    But it is late here…. :-)

    By David Leedy on May 21, 2012

  3. Absolutely true, you can put an object into a map etc.
    The advantage of the MultiMap ( to me ) is that you do not have to care about the logic of how to add the values to the map.
    Using the MultiMap is a more intuitive way to do this. Adding to a map is map.put(key, object) and so is multimap.put(key, object).
    Without the multiMap you would have to check, if the key already exists and then add another object to an ( existing ) object for this key. Code, you would have to write by yourself. :)

    By Ulrich Krause on May 21, 2012

  4. They say that guava from google is a tad “better” alternative for jakarta commons this days. Personally, I haven’t find much use for the jakarta commons except StringUtils in my projects.
    You get this multivalue feature with a simple method like this:

    private void addMultivalue(Map<String, Set> map, String key, String value) {
    if ((map==null) ||(key == null) || (value == null)) throw new IllegalArgumentException(“None of this arguments should be null: map=” + map + “, key=” + key + “, value=” + value);
    Set values = map.get(key);
    if (values == null) {
    values = new HashSet();
    map.put(key, values);
    } else {


    Sometimes its questionable if its really worth to add a new 3rd party lib. Well, I allways try to convince people to add this, though:

    By axel on May 22, 2012

Sorry, comments for this entry are closed at this time.