如何将多个值存储到一个键(java) [英] How to store several values to one key (java)
问题描述
我搜索一个数据结构,可以在其中存储几个键值对.
I search for a datastructure, where I can store several key-value pairs.
数据本质上是这样的:
(1 , value_1)
(2 , value_2)
所以我想到了使用HashMap.遗憾的是,这对我不起作用,因为一个键可能会出现多个值.
So I thought of using HashMap. Sadly this won't work for me, because multiple values to one key can occur.
(在上面的示例中:
(1 , value_2)
可能是另一个条目
除了用新的Object或类似的东西创建一个List之外,还有什么方法可以有效地存储这些数据.
Is there any way of performantly storing this data, except creating a List with a new Object or something like this.
get(1)
应将value_1和value_2作为列表或集合或类似内容返回.
should return value_1 and value_2 as a list or set or anything similar.
预先感谢
推荐答案
我认为您要查找的数据结构位于Google的番石榴库MultiMap中.参见 http://guava-libraries.googlecode .com/svn-history/r13/trunk/javadoc/com/google/common/collect/Multimap.html .
I think the data strucure you're looking for is in google's guava library, MultiMap. See http://guava-libraries.googlecode.com/svn-history/r13/trunk/javadoc/com/google/common/collect/Multimap.html.
基本上是Map<K,Collection<V>>
,但具有易于使用的界面.
Basically it's a Map<K,Collection<V>>
but with an easier to use interface.
这篇关于如何将多个值存储到一个键(java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!