MultiMap vs MultiValue Map

In one place I need to use a map with many values ​​mapped to a single key, so I was wondering if there is a significant performance difference between using the HashMap key, list and MultiMap key, values in java.

+7
source share
4 answers

You can try, but I doubt there is a big difference as it does the same.

IMHO. The advantage is simpler / clearer code, which is usually more important than performance.

+6
source

I would recommend using Google collections if you want to use a more convenient implementation of Multimap. If you don't want to introduce a new dependency, HashMap<Key, Collection<Value>> should do the trick, which is very different from apache.collections HashMultiMap.

+1
source

Hash provides O (1), which is fast and does nothing with the size of the elements.

As for Multimap, you can put values ​​in a dependent collection (List, Set). Various collection implementations provide excellent performance.

EDIT: As I commented on Sebastian's answer. You can use Guava, which provides various options for collecting values: HashMultiMap (HashMap<KEY, HashSet<VALUE>>) , ArrayListMultiMap (HashMap<KEY, ArrayList<VALUE>>) ...

0
source

If it is Map Key-> Values, use the Map implementation.

Since you will have some values ​​with the same keys, use http://guava-libraries.googlecode.com/svn/tags/release09/javadoc/com/google/common/collect/HashMultiset.html from the Google collection (now this is a library guava, http://code.google.com/p/guava-libraries/ ) for your task.

0
source

All Articles