If I want to create a new Multimap with simple defaults, I curently need to do something like:
如果我想用简单的默认值创建一个新的Multimap,我需要做类似的事情:
private final Multimap<Key, Value> providersToClasses = Multimaps
.newListMultimap(
new HashMap<Key, Collection<Value>>(),
new Supplier<List<Value>>() {
@Override
public List<Value> get() {
return Lists.newArrayList();
}
});
because Java can't infer the correct types if Maps.newHashMap
is used for the backing map. Of course, this can be refactored into a separate method, but is there already a way to write it more concisely?
因为如果将Maps.newHashMap用于支持映射,Java无法推断出正确的类型。当然,这可以重构为一个单独的方法,但是有没有办法更简洁地编写它?
6 个解决方案
#1
52
Why aren't you using ArrayListMultimap.create()
for such a simple case? It's the default way to create the simple HashMap/ArrayList that is probably the most common used multimap.
你为什么不使用ArrayListMultimap.create()这么简单的情况?这是创建简单HashMap / ArrayList的默认方式,它可能是最常用的多重映射。
#2
9
I run into this problem when writing clients and building up maps of query params. A nice succinct pattern I like for constructing multi-maps is to use ImmutableMultiMap#builder
我在编写客户端和构建查询参数的映射时遇到了这个问题。我喜欢构建多地图的一个很好的简洁模式是使用ImmutableMultiMap#builder
Multimap<String, String> queryParams =
ImmutableMultimap.<String, String>builder()
.put("key-1", "value-1")
.put("key-1", "value-2")
.build();
#3
8
The Guava documentation states that the create
method advocated by some other answers "will soon be deprecated" in favour of the different forms presented below, and should therefore be avoided.
Guava文档指出,其他一些答案提倡的创建方法“将很快被弃用”,以支持下面提供的不同形式,因此应该避免。
From Guava 21.0 onwards, the recommended way of creating a Multimap
object where values are stored in ArrayList
collections is the following:
从Guava 21.0开始,创建Multimap对象的建议方法是将值存储在ArrayList集合中,如下所示:
MultimapBuilder.hashKeys().arrayListValues().build();
You can also use parameters if you want to specify the expected number of keys in your map and the expected number of values per key:
如果要指定地图中预期的键数以及每个键的预期值数,也可以使用参数:
MultimapBuilder.hashKeys(expectedKeys).arrayListValues(expectedValuesPerKey).build();
Finally, you can create a new Multimap
from an existing one using this construct:
最后,您可以使用此构造从现有Multimap创建新的Multimap:
MultimapBuilder.hashKeys().arrayListValues().build(multimap);
If you want to use data structures other than ArrayLists
in your Multimap
, you can replace the call to arrayListValues()
by a number of other ones, listed here.
如果要在Multimap中使用ArrayLists以外的数据结构,可以使用此处列出的许多其他数据结构替换对arrayListValues()的调用。
#4
4
In Java 8 this is much nicer, for all kinds of multimaps. This is for two reasons:
在Java 8中,对于所有类型的多图,这都更好。这有两个原因:
- Because the compiler uses target types for type inference, you don't have to provide explicit type arguments for the
HashMap
contructor. -
Constructor method references can be used to create the
ArrayList
factory.
由于编译器使用目标类型进行类型推断,因此您不必为HashMap构造函数提供显式类型参数。
构造方法引用可用于创建ArrayList工厂。
It looks like this:
它看起来像这样:
Multimap<Key, Value> providersToClasses =
Multimaps.newListMultimap(new HashMap<>(), ArrayList::new);
#5
4
Here is compact solution:
这是紧凑的解决方案:
Multimap<Integer, String> multi = HashMultimap.create();
#6
2
To answer the original type inference problem, though, you can also specify the generic types on a static method using Maps.<Key, Collection<Value>>newHashMap()
, but it's certainly not more concise than new HashMap<Key, Collection<Value>>()
(it may be more consistent).
但是,要回答原始类型推断问题,您还可以使用Maps。
#1
52
Why aren't you using ArrayListMultimap.create()
for such a simple case? It's the default way to create the simple HashMap/ArrayList that is probably the most common used multimap.
你为什么不使用ArrayListMultimap.create()这么简单的情况?这是创建简单HashMap / ArrayList的默认方式,它可能是最常用的多重映射。
#2
9
I run into this problem when writing clients and building up maps of query params. A nice succinct pattern I like for constructing multi-maps is to use ImmutableMultiMap#builder
我在编写客户端和构建查询参数的映射时遇到了这个问题。我喜欢构建多地图的一个很好的简洁模式是使用ImmutableMultiMap#builder
Multimap<String, String> queryParams =
ImmutableMultimap.<String, String>builder()
.put("key-1", "value-1")
.put("key-1", "value-2")
.build();
#3
8
The Guava documentation states that the create
method advocated by some other answers "will soon be deprecated" in favour of the different forms presented below, and should therefore be avoided.
Guava文档指出,其他一些答案提倡的创建方法“将很快被弃用”,以支持下面提供的不同形式,因此应该避免。
From Guava 21.0 onwards, the recommended way of creating a Multimap
object where values are stored in ArrayList
collections is the following:
从Guava 21.0开始,创建Multimap对象的建议方法是将值存储在ArrayList集合中,如下所示:
MultimapBuilder.hashKeys().arrayListValues().build();
You can also use parameters if you want to specify the expected number of keys in your map and the expected number of values per key:
如果要指定地图中预期的键数以及每个键的预期值数,也可以使用参数:
MultimapBuilder.hashKeys(expectedKeys).arrayListValues(expectedValuesPerKey).build();
Finally, you can create a new Multimap
from an existing one using this construct:
最后,您可以使用此构造从现有Multimap创建新的Multimap:
MultimapBuilder.hashKeys().arrayListValues().build(multimap);
If you want to use data structures other than ArrayLists
in your Multimap
, you can replace the call to arrayListValues()
by a number of other ones, listed here.
如果要在Multimap中使用ArrayLists以外的数据结构,可以使用此处列出的许多其他数据结构替换对arrayListValues()的调用。
#4
4
In Java 8 this is much nicer, for all kinds of multimaps. This is for two reasons:
在Java 8中,对于所有类型的多图,这都更好。这有两个原因:
- Because the compiler uses target types for type inference, you don't have to provide explicit type arguments for the
HashMap
contructor. -
Constructor method references can be used to create the
ArrayList
factory.
由于编译器使用目标类型进行类型推断,因此您不必为HashMap构造函数提供显式类型参数。
构造方法引用可用于创建ArrayList工厂。
It looks like this:
它看起来像这样:
Multimap<Key, Value> providersToClasses =
Multimaps.newListMultimap(new HashMap<>(), ArrayList::new);
#5
4
Here is compact solution:
这是紧凑的解决方案:
Multimap<Integer, String> multi = HashMultimap.create();
#6
2
To answer the original type inference problem, though, you can also specify the generic types on a static method using Maps.<Key, Collection<Value>>newHashMap()
, but it's certainly not more concise than new HashMap<Key, Collection<Value>>()
(it may be more consistent).
但是,要回答原始类型推断问题,您还可以使用Maps。