Java中map<String, List<String>>的实例化方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18220443/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:25:58  来源:igfitidea点击:

The way to instantiate map<String, List<String>> in Java

javalistmapgeneric-programming

提问by Alfred Zhong

I would like to instantiate Map<String, List<String>>in Java,

我想Map<String, List<String>>在 Java 中实例化,

I tried

我试过

Map<String, List<String>> foo = new <String, List<String>>();

and

Map<String, List<String>> foo = new <String, ArrayList<String>>();

None of them work. Does any one know how to instantiate this map in Java?

他们都没有工作。有谁知道如何在 Java 中实例化这张地图?

回答by Xabster

new HashMap<String, List<String>>();

or as gparyani commented:

或正如 gparyani 评论的那样:

new HashMap<>(); // type inference

Note: each entry needs to be given an instantiated List as a value. You cannot get("myKey").add("some_string_for_this_key"); the very first time you get() a List from it.

注意:每个条目都需要一个实例化的 List 作为值。你不能 get("myKey").add("some_string_for_this_key"); 第一次从它获取()一个列表。

So, fetch a List, check if it's null.

因此,获取一个列表,检查它是否为空。

If it's null, make a new list, add the string to it, put the List back. If it's anything but null, add to it, or do what you want.

如果它为空,则创建一个新列表,向其中添加字符串,然后将列表放回原处。如果它不是空值,请添加它,或者做你想做的。

回答by Ravi Thapliyal

You forgot to mention the class. Maphere is the reference type and is an Interface. HashMapon the other side of equals specifies the actualtype of the Objectcreated and assigned to the reference foo.

你忘了提到课程。Map这里是引用类型,是一个InterfaceHashMap在 equals 的另一侧指定创建并分配给引用的Object实际类型。foo

Map<String, List<String>> foo = new HashMap<String, List<String>>();

The actual type specified (HashMaphere) must be assignableto the reference type (Maphere) i.e. if the type of reference is an Interface, the Object's type must implementit. And, if the type of the reference is a Class, the Object's type must either be the same classor its subtypei.e. it extends from it.

指定的实际类型(HashMap此处)必须可分配给引用类型(Map此处),即如果引用类型是Interface,则 Object 的类型必须实现它。并且,如果引用的类型是Class,则 Object 的类型必须是相同的或其子类型,即它从它扩展。

From Java 7 onwards, you can use a shorthand like

从 Java 7 开始,您可以使用像

Map<String, List<String>> foo = new HashMap<>();

Your second way of instantiation is notrecommended. Stick to using Listwhich is an Interface.

推荐您的第二种实例化方式。坚持使用Listwhich 是一个Interface

// Don't bind your Map to ArrayList
new TreeMap<String, ArrayList<String>>();

// Use List interface type instead
new TreeMap<String, List<String>>();

回答by Willis Blackburn

Map is an interface. You have to tell Java which concrete Map class you want to instantiate.

地图是一个界面。你必须告诉 Java 你想实例化哪个具体的 Map 类。

Map<String, List<String>> foo = new HashMap<String, List<String>>();

or

或者

Map<String, List<String>> foo = new TreeMap<String, List<String>>();

etc.

等等。