java 什么java集合为同一个键提供多个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6850901/
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 java collection that provides multiple values for the same key
提问by palAlaa
what type of java collection that returns multiple values for the same key?
什么类型的java集合为同一个键返回多个值?
example, I want to return 301,302,303 for key 300.
例如,我想为键 300 返回 301,302,303。
回答by Jo?o Silva
You can use a List
as the value of your Map
:
您可以使用 aList
作为您的值Map
:
List<Integer> list = new ArrayList<Integer>();
list.add(301);
list.add(302);
list.add(303);
Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
map.put(300, list);
map.get(300); // [301,302,303]
Alternatively, you can use Multimap
from Guava, as suggested by biziclop, which has a much cleaner syntax, and lots of other very useful utility methods:
或者,您可以使用Multimap
来自 Guava,正如 biziclop 所建议的那样,它具有更清晰的语法,以及许多其他非常有用的实用方法:
Multimap<Integer, Integer> map = HashMultimap.create();
map.put(300, 301);
map.put(300, 302);
map.put(300, 303);
Collection<Integer> list = map.get(300); // [301, 302, 303]
回答by Bernd Elkemann
You could use Multimap, it is under the Apache license.
您可以使用 Multimap,它在 Apache 许可下。
See this link. For posterity:
请参阅此链接。为后人:
org.apache.commons.collections
Interface MultiMap
All Superinterfaces:
java.util.Map
All Known Implementing Classes:
MultiHashMap, MultiValueMap
public interface MultiMap
extends java.util.Map
Defines a map that holds a collection of values against each key.
A MultiMap is a Map with slightly different semantics. Putting a value into the map will add the value to a Collection at that key. Getting a value will return a Collection, holding all the values put to that key.
For example:
MultiMap mhm = new MultiHashMap();
mhm.put(key, "A");
mhm.put(key, "B");
mhm.put(key, "C");
Collection coll = (Collection) mhm.get(key);
coll will be a collection containing "A", "B", "C".
回答by Tarun
As mentioned in a comment above above there is always Guava Multimap
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Multimap.htmlApache Commons Collections 4 has Generic version of MultiMap http://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/MultiMap.html
JAX-RS specifies a MultivaluedMap interface which is implemented by all JAX-RS Providers. If your use case is within the context of a JAX-RS REST Service/Client, it would be an option to use their implementation without pulling in another dependency.
javax.ws.rs.core.MultivaluedMap (each JAX RS Provider has their own implementation)
正如上面的评论中提到的,总是有 Guava Multimap
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Multimap.htmlApache Commons Collections 4 有通用版本的 MultiMap http://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/MultiMap.html
JAX-RS 指定了一个由所有 JAX-RS 提供者实现的 MultivaluedMap 接口。如果您的用例在 JAX-RS REST 服务/客户端的上下文中,则可以选择使用它们的实现而不引入其他依赖项。
javax.ws.rs.core.MultivaluedMap(每个 JAX RS Provider 都有自己的实现)