Java HashMap 和 HashMultimap 有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19222029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is difference between HashMap and HashMultimap
提问by Faisal Basra
I see many examples about multimap but did not understand why Google Gauva is different?
我看到很多关于 multimap 的例子,但不明白为什么 Google Gauva 不同?
Multimap<Integer, Set<String>> option4 = HashMultimap.create(); // Gauva
Map<Integer, Set<String>> opt = new HashMap<Integer, Set<String>>(); //Core Java
Is both above are behave same for holding data or different?
以上两者对于保存数据的行为是否相同或不同?
采纳答案by JB Nizet
A MultiMap<A, B>
associates a key of type A with a value of type Collection<B>
(hence the name MultiMap)
AMultiMap<A, B>
将类型 A 的键与类型的值相关联Collection<B>
(因此命名为 MultiMap)
A Map<A, B>
associates a key of type A with a value of type B.
AMap<A, B>
将类型 A 的键与类型 B 的值相关联。
So, a MultiMap<Integer, Set<String>>
can be viewed as a Map<Integer, Collection<Set<String>>
. This should be obvious by reading the api documentation.
因此, aMultiMap<Integer, Set<String>>
可以被视为 a Map<Integer, Collection<Set<String>>
。通过阅读api 文档,这应该是显而易见的。
回答by Admit
Nope, MultiMap
means that there would be a collection of objects attached to each key.
Documentation: Multimap_Is_Not_A_Map
不,这MultiMap
意味着每个键都会附加一组对象。
文档:Multimap_Is_Not_A_Map
回答by Rich
The difference is that with the second, Core Java implementation, you need to check whether the Set is there before you insert. Guava's Multimap takes care of that for you.
不同之处在于,对于第二个 Core Java 实现,您需要在插入之前检查 Set 是否存在。Guava 的 Multimap 会为您处理这些。
With Core Java:
使用核心 Java:
Set<String> innerSet = opt.get(key);
if (innerSet == null) {
innerSet = new HashSet<String>();
opt.put(key, innerSet);
}
innerSet.add(value);
With Guava:
番石榴:
opt.put(key, value);
Guava takes care of initialising an otherwise absent Set to store the values, takes care of any threading issues (eg stops two threads from creating a new Set for the same key in parallel) and also provides a few useful methods that you'd otherwise need to implement by hand, such as getting all the values across all the Set
s.
Guava 负责初始化一个原本不存在的 Set 来存储值,处理任何线程问题(例如,阻止两个线程并行地为同一个键创建新的 Set),并且还提供了一些您需要的有用方法手动实现,例如获取所有Set
s 的所有值。
回答by Evgeniy Dorofeev
First of all com.google.common.collect.Multimap is not java.util.Map, it is in a separate hierarchy.
首先 com.google.common.collect.Multimap 不是 java.util.Map,它在一个单独的层次结构中。
Secondly, you can all the operations with Map<Integer, Set<String>>
that Multimap interface requires but you will have to implement them yourself while HashMultimap offers ready implementation.
其次,您可以使用Map<Integer, Set<String>>
Multimap 接口所需的所有操作,但您必须自己实现它们,而 HashMultimap 提供了现成的实现。
回答by Sean Patrick Floyd
You misunderstood something. These are not even roughly equivalent:
你误会了什么。这些甚至不大致相等:
Multimap<Integer, Set<String>> option4 = HashMultimap.create(); // Guava
Map<Integer, Set<String>> opt = new HashMap<Integer, Set<String>>(); //Core Java
In your example, opt4
would map a single Integer
to a Collection of Sets of Strings.
That's exactly the point of using a Multimap
, you don't have to explicitly deal with the second dimension. So in fact, the correct (equivalent) declaration would be:
在您的示例中,opt4
将单个映射Integer
到一组字符串集合。这正是使用 a 的重点Multimap
,您不必明确处理第二个维度。所以实际上,正确的(等效的)声明是:
SetMultimap<Integer, String> multimap = HashMultimap.create(); // Guava
and you can get a map view like this:
你可以得到这样的地图视图:
Map<Integer, Set<String>> mapView = multimap.asMap();