I didn't find such a multimap construction... When I want to do this, I iterate over the map, and populate the multimap. Is there an other way?
我没有找到这么多地图的构造…当我想这样做时,我遍历映射,并填充multimap。还有别的办法吗?
final Map<String, Collection<String>> map = ImmutableMap.<String, Collection<String>>of(
"1", Arrays.asList("a", "b", "c", "c"));
System.out.println(Multimaps.forMap(map));
final Multimap<String, String> expected = ArrayListMultimap.create();
for (Map.Entry<String, Collection<String>> entry : map.entrySet()) {
expected.putAll(entry.getKey(), entry.getValue());
}
System.out.println(expected);
The first result is {1=[[a, b, c, c]]}
but I expect {1=[a, b, c, c]}
第一个结果是{1=[[a, b, c, c]]},但我期望{1=[a, b, c, c]}
5 个解决方案
#1
44
Assuming you have
假设您已经
Map<String, Collection<String>> map = ...;
Multimap<String, String> multimap = ArrayListMultimap.create();
Then I believe this is the best you can do
我相信这是你能做到的最好的。
for (String key : map.keySet()) {
multimap.putAll(key, map.get(key));
}
or the more optimal, but harder to read
或者更优,但更难以阅读。
for (Entry<String, Collection<String>> entry : map.entrySet()) {
multimap.putAll(entry.getKey(), entry.getValue());
}
#2
7
Here is a useful generic version that I wrote for my StuffGuavaIsMissing class.
这里有一个有用的通用版本,我为我的StuffGuavaIsMissing类编写了这个版本。
/**
* Creates a Guava multimap using the input map.
*/
public static <K, V> Multimap<K, V> createMultiMap(Map<K, ? extends Iterable<V>> input) {
Multimap<K, V> multimap = ArrayListMultimap.create();
for (Map.Entry<K, ? extends Iterable<V>> entry : input.entrySet()) {
multimap.putAll(entry.getKey(), entry.getValue());
}
return multimap;
}
And an immutable version:
和一个不可变的版本:
/**
* Creates an Immutable Guava multimap using the input map.
*/
public static <K, V> ImmutableMultimap<K, V> createImmutableMultiMap(Map<K, ? extends Iterable<V>> input) {
ImmutableMultimap.Builder<K, V> builder = ImmutableMultimap.builder();
for (Map.Entry<K, ? extends Iterable<V>> entry : input.entrySet()) {
builder.putAll(entry.getKey(), entry.getValue());
}
return builder.build();
}
#3
4
This question is a little old, but I thought I'd give an updated answer. With Java 8 you could do something along the lines of
这个问题有点老了,但我想我会给出一个更新的答案。用Java 8,你可以做一些事情。
ListMultimap<String, String> multimap = ArrayListMultimap.create();
Map<String, Collection<String>> map = ImmutableMap.of(
"1", Arrays.asList("a", "b", "c", "c"));
map.forEach(multimap::putAll);
System.out.println(multimap);
This should give you {1=[a, b, c, c]}
, as desired.
这将根据需要给您{1=[a, b, c, c]}。
#4
2
UPDATE: For what you're asking, I think you're going to need to fall back to Multimap.putAll
.
更新:对于你所问的问题,我认为你需要回到Multimap.putAll。
#5
1
Following code without Google's Guava library. It is used for double value as key and sorted order
以下代码没有谷歌的Guava库。它被用于作为键和排序顺序的双值。
Map<Double,List<Object>> multiMap = new TreeMap<Double,List<Object>>();
for( int i= 0;i<15;i++)
{
List<Object> myClassList = multiMap.get((double)i);
if(myClassList == null)
{
myClassList = new ArrayList<Object>();
multiMap.put((double) i,myClassList);
}
myClassList.add("Value "+ i);
}
List<Object> myClassList = multiMap.get((double)0);
if(myClassList == null)
{
myClassList = new ArrayList<Object>();
multiMap.put( (double) 0,myClassList);
}
myClassList.add("Value Duplicate");
for (Map.Entry entry : multiMap.entrySet())
{
System.out.println("Key = " + entry.getKey() + ", Value = " +entry.getValue());
}
#1
44
Assuming you have
假设您已经
Map<String, Collection<String>> map = ...;
Multimap<String, String> multimap = ArrayListMultimap.create();
Then I believe this is the best you can do
我相信这是你能做到的最好的。
for (String key : map.keySet()) {
multimap.putAll(key, map.get(key));
}
or the more optimal, but harder to read
或者更优,但更难以阅读。
for (Entry<String, Collection<String>> entry : map.entrySet()) {
multimap.putAll(entry.getKey(), entry.getValue());
}
#2
7
Here is a useful generic version that I wrote for my StuffGuavaIsMissing class.
这里有一个有用的通用版本,我为我的StuffGuavaIsMissing类编写了这个版本。
/**
* Creates a Guava multimap using the input map.
*/
public static <K, V> Multimap<K, V> createMultiMap(Map<K, ? extends Iterable<V>> input) {
Multimap<K, V> multimap = ArrayListMultimap.create();
for (Map.Entry<K, ? extends Iterable<V>> entry : input.entrySet()) {
multimap.putAll(entry.getKey(), entry.getValue());
}
return multimap;
}
And an immutable version:
和一个不可变的版本:
/**
* Creates an Immutable Guava multimap using the input map.
*/
public static <K, V> ImmutableMultimap<K, V> createImmutableMultiMap(Map<K, ? extends Iterable<V>> input) {
ImmutableMultimap.Builder<K, V> builder = ImmutableMultimap.builder();
for (Map.Entry<K, ? extends Iterable<V>> entry : input.entrySet()) {
builder.putAll(entry.getKey(), entry.getValue());
}
return builder.build();
}
#3
4
This question is a little old, but I thought I'd give an updated answer. With Java 8 you could do something along the lines of
这个问题有点老了,但我想我会给出一个更新的答案。用Java 8,你可以做一些事情。
ListMultimap<String, String> multimap = ArrayListMultimap.create();
Map<String, Collection<String>> map = ImmutableMap.of(
"1", Arrays.asList("a", "b", "c", "c"));
map.forEach(multimap::putAll);
System.out.println(multimap);
This should give you {1=[a, b, c, c]}
, as desired.
这将根据需要给您{1=[a, b, c, c]}。
#4
2
UPDATE: For what you're asking, I think you're going to need to fall back to Multimap.putAll
.
更新:对于你所问的问题,我认为你需要回到Multimap.putAll。
#5
1
Following code without Google's Guava library. It is used for double value as key and sorted order
以下代码没有谷歌的Guava库。它被用于作为键和排序顺序的双值。
Map<Double,List<Object>> multiMap = new TreeMap<Double,List<Object>>();
for( int i= 0;i<15;i++)
{
List<Object> myClassList = multiMap.get((double)i);
if(myClassList == null)
{
myClassList = new ArrayList<Object>();
multiMap.put((double) i,myClassList);
}
myClassList.add("Value "+ i);
}
List<Object> myClassList = multiMap.get((double)0);
if(myClassList == null)
{
myClassList = new ArrayList<Object>();
multiMap.put( (double) 0,myClassList);
}
myClassList.add("Value Duplicate");
for (Map.Entry entry : multiMap.entrySet())
{
System.out.println("Key = " + entry.getKey() + ", Value = " +entry.getValue());
}