如何创建多重贴图来自地图>>?

发布于 2024-09-06 14:08:34 字数 617 浏览 8 评论 0 原文

我没有找到这样的多重映射构造...当我想这样做时,我会迭代映射,并填充多重映射。还有其他办法吗?

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);

第一个结果是 {1=[[a, b, c, c]]} 但我期望 {1=[a, b, c, c]}

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?

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]}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

百合的盛世恋 2024-09-13 14:08:34

假设你有

Map<String, Collection<String>> map = ...;
Multimap<String, String> multimap = ArrayListMultimap.create();

那么我相信这是你能做的最好的

for (String key : map.keySet()) {
  multimap.putAll(key, map.get(key));
}

或更优化的,但更难阅读

for (Entry<String, Collection<String>> entry : map.entrySet()) {
  multimap.putAll(entry.getKey(), entry.getValue());
}

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());
}
[浮城] 2024-09-13 14:08:34

这个问题有点老了,但我想我会给出一个更新的答案。使用 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 应该为您提供 {1=[a, b, c, c]} 的操作。

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

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.

伴我老 2024-09-13 14:08:34

这是我为我的 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;
}

还有一个不可变的版本:

/**
 * 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();
}

Here is a useful generic version that I wrote for my StuffGuavaIsMissing class.

/**
 * 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();
}
苏璃陌 2024-09-13 14:08:34

更新:对于你所问的问题,我认为你需要回到 Multimap.putAll

UPDATE: For what you're asking, I think you're going to need to fall back to Multimap.putAll.

无妨# 2024-09-13 14:08:34

您可以使用 com.google.common.collect.Multimaps.flatteningToMultimap 作为流收集器。

给定地图:

Map<Integer, Collection<String>> map = Map.of(
   1, List.of("a", "b", "c"),
   2, List.of("d", "e", "f"));

您可以执行以下操作:

Multimap<Integer, String> multimap = map.entrySet().stream()
    .collect(flatteningToMultimap(Map.Entry::getKey, it -> it.getValue().stream(), HashMultimap::create));

You can use the com.google.common.collect.Multimaps.flatteningToMultimap as your stream collector.

Given map:

Map<Integer, Collection<String>> map = Map.of(
   1, List.of("a", "b", "c"),
   2, List.of("d", "e", "f"));

You can do the following:

Multimap<Integer, String> multimap = map.entrySet().stream()
    .collect(flatteningToMultimap(Map.Entry::getKey, it -> it.getValue().stream(), HashMultimap::create));
南巷近海 2024-09-13 14:08:34

以下代码没有 Google 的 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());
}

Following code without Google's Guava library. It is used for double value as key and sorted order

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());
}
坦然微笑 2024-09-13 14:08:34
LinkedListMultimap<String, String> mm = map.entrySet()
    .stream()
    .collect(
        () -> LinkedListMultimap.create(map.size()),
        (m, e) -> m.putAll(e.getKey(), e.getValue()),
        (m1, m2) -> m1.putAll(m2));
LinkedListMultimap<String, String> mm = map.entrySet()
    .stream()
    .collect(
        () -> LinkedListMultimap.create(map.size()),
        (m, e) -> m.putAll(e.getKey(), e.getValue()),
        (m1, m2) -> m1.putAll(m2));
万人眼中万个我 2024-09-13 14:08:34

还有另一种方法可以使用:
Multimaps#newMultimap

如果您使用扩展 ListSet 的集合,则文档建议使用特殊方法 Multimaps#newListMultimapMultimaps#newSetMultimap

import com.google.common.collect.Multimaps;

Multimaps.newListMultimap(myMap, ArrayList::new);
Multimaps.newSetMultimap(myMap, HashSet::new);

There is another method one can use:
Multimaps#newMultimap

If you are using a collection extending List or Set, the docs recommend using the special methods Multimaps#newListMultimap and Multimaps#newSetMultimap.

import com.google.common.collect.Multimaps;

Multimaps.newListMultimap(myMap, ArrayList::new);
Multimaps.newSetMultimap(myMap, HashSet::new);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文