Search code examples
javaandroidmultimap

How can I create a Multimap in Java on Android


Where can I find an implementation of multimap for Java that will work on Android without having to include any other classes etc. The implementations I've found all require other things which require other things and it gets messy quick. I'm porting a project from C++ and am new to java (and this project as well so I'm trying to keep things as identical as possible while getting it working) so any references or examples would be great.


Solution

  • You could use a Map and a List to create a multimap. Say you wanted to associate an integer with a list of class type T. Use the following code:

    Map<Integer, List<T>> myMultiMap = new HashMap<Integer, List<T>>();

    It could get messy but, this should give you what you're looking for.

    Another option is to use the Guava library's Multimap implementations.