Java Parameterised MultiMap Implementation
A MultiMap is essentially an augmented data structure for storing key-value pairs where one key can potentially be associated with any number of values. I’ve accomplished this in the past by using a Map<Key, List<Value>> or some variation in the past, but methods of access are inconvenient and can be a source of many bugs. Surprisingly, I’ve never gone out of my way to implement what should be a relatively simple data structure, but I finally did it for my most recent project, so I’m making it available to anyone who wants to use it.
NOTE: I have improved the implementation and released a new version which can be found here.
I’ve recently been working on a project using Google Web Toolkit, and started implementing a simple MVC framework when I came upon the need to implement a MultiMap. At first, I started using my old method as recommended at the bottom of this page, but I wanted it to be a little more convenient and less error prone.
So I started looking for some implementation online, but couldn’t find anything that took advantage of parameterised types, or that offered all the features I wanted. Also, I’ve recently been dabbling in TDD and wanted to use this as a little more practice before I moved on to bigger-scale projects.
As a result, the MultiMap implementation has been thoroughly tested, and I am quite certain that it works as expected.
You can download the multimap.jar here (there’s only one class and a JUnit test class) or if you prefer you can download the MultiMap.java source file.
The use of the class is incredibly simple and straightforward. The JavaDoc should cover everything you might want to do.
If you’d rather learn by example (which is sometimes easier), look at this Java file.




