简体   繁体   English

Java 使用多图作为参数

[英]Java using a multimap as a parameter

I wanted to create a multimap for each of the keys that I put, but I get this error Multimap is abstract; cannot be instantiated.我想为我放置的每个键创建一个多重映射,但我得到这个错误Multimap is abstract; cannot be instantiated. Multimap is abstract; cannot be instantiated. Is there another way to do this?还有另一种方法可以做到这一点吗?

package Graph;


import com.google.common.collect.Multimap;
import java.util.HashMap;

public class builder {

    public static void main(String[] args) {
        
        HashMap<String,Multimap<String,String>> map = new HashMap<>();
        map.put("Cars",new Multimap<String,String>());
        
    }
}

Interface, not a class接口,不是 class

MultiMap in Google Guava is an interface. Google Guava中的MultiMap是一个界面。 You cannot instantiate an interface.您不能实例化接口。

Choose a concrete implementation of that interface to instantiate.选择该接口的具体实现来实例化。

Guava supplies at least a dozen.番石榴至少供应一打。 And third-parties may also supply implementations (I don't know of any).第三方也可能提供实现(我不知道)。

By the way, Eclipse Collections also provides multi-map functionality.顺便说一句, Eclipse Collections还提供了多地图功能。

Here are the few things about Google's Multimap interface.以下是关于 Google 的 Multimap 界面的一些内容。

Note 0注释 0

Multimap is a collection-type data structure which can store more than 1 values against a key. Multimap 是一种集合类型的数据结构,可以针对一个键存储多个值。

    multimap.get(0).add("Soumyajit");        
    multimap.get(0).add("Chatterjee");        
    System.out.println(multimap);

will returns: {0=[Soumyajit, Chatterjee]} as output.将返回: {0=[Soumyajit, Chatterjee]}作为 output。

Note 1注1

It has two child interfaces ListMultimap and SetMultimap .它有两个子接口ListMultimapSetMultimap we can use Multimap's static method to create classes that will implement Multimap interface ie, ArrayListMultimap<K, V>, LinkedListMultimap<K, V> etc.我们可以使用 Multimap 的static方法来创建将实现Multimap接口的类,即ArrayListMultimap<K, V>, LinkedListMultimap<K, V>等。

example: ListMultimap<String, Integer> m = ArrayListMultimap.create();示例: ListMultimap<String, Integer> m = ArrayListMultimap.create();

Note 2笔记2

We can get values from Multimap collection against a key in the form of collection using: Multimap.get(key) ;我们可以使用Multimap.get(key)以集合的形式从 Multimap 集合中针对键获取值: which returns value if present with its reference so if we modify the returned result set it will reflect back to original Map.如果存在其引用,则返回值,因此如果我们修改返回的结果集,它将反映回原始 Map。 If no value found for specific key, it will return an empty collection (Not NULL).如果没有找到特定键的值,它将返回一个空集合(非空)。

Therefore, the following code is absolutely fine:因此,下面的代码绝对没问题:

        ListMultimap<Integer, String> multimap =  ArrayListMultimap.create();        
      multimap.get(0).add("Soumyajit");        
      multimap.get(4).add("Chatterjee");        

Output: {0=[Soumyajit], 4=[Chatterjee]} Output: {0=[Soumyajit], 4=[Chatterjee]}

Note 3注3

To add data in Multimap we can follow two ways:要在 Multimap 中添加数据,我们可以采用两种方式:

map.get(index).add(value);
   or
map.put(index, value);

For deleting, we will follow same structure:对于删除,我们将遵循相同的结构:

   map.get(index).remove(value);
     or
   map.remove(index, value);

Reference taken from: Official Google's Official Guava documentation参考取自: Google 官方 Guava 官方文档

As mentioned by others, Multimap is an interface.正如其他人所提到的, Multimap是一个接口。 You will need to use concrete classes such as HashMultimap or ImmutableMultimap .您将需要使用具体的类,例如HashMultimapImmutableMultimap See below example:请参见下面的示例:

HashMap<String, Multimap<String, String>> maps = new HashMap<>();

Multimap<String, String> multimap = HashMultimap.create();
multimap.put("KEY1", "V1");
    
maps.put("KEY", multimap);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM