MultiMap vs MultiValue Map

后端 未结 4 1519
悲哀的现实
悲哀的现实 2021-01-17 21:40

At one place i have to use a map with many values mapped to a single key, so i was wondering whether there is any significant performance distinction between using H

相关标签:
4条回答
  • 2021-01-17 22:17

    You can try it but I doubt there is much difference as it does much the same thing.

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

    0 讨论(0)
  • 2021-01-17 22:18

    I'd recommend to use google collections if you want to use a more convenient implementation of a Multimap. In case you don't want to introduce a new dependency, HashMap<Key, Collection<Value>> should do the trick which is pretty much what apache.collections HashMultiMap does.

    0 讨论(0)
  • 2021-01-17 22:29

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

    Regarding to Multimap, you could put values in dependent collection (List, Set). Different collection implementations provides different performance.

    EDIT: As I commented on Sebastian's answer. You could use Guava which provides different value collection implemantions: HashMultiMap (HashMap<KEY, HashSet<VALUE>>), ArrayListMultiMap (HashMap<KEY, ArrayList<VALUE>>)...

    0 讨论(0)
  • 2021-01-17 22:37

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

    As you will have some Values with the same Keys, use the http://guava-libraries.googlecode.com/svn/tags/release09/javadoc/com/google/common/collect/HashMultiset.html from the Google Collection (now guava library, http://code.google.com/p/guava-libraries/ ) for your task.

    0 讨论(0)
提交回复
热议问题