简体   繁体   English

实例化地图的方式 <String, List<String> &gt;在Java中

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

I would like to instantiate Map<String, List<String>> in Java, 我想在Java中实例化Map<String, List<String>>

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中实例化这个地图?

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. 注意:每个条目都需要以实例化的List作为值。 You cannot get("myKey").add("some_string_for_this_key"); 你不能得到(“myKey”)。add(“some_string_for_this_key”); the very first time you get() a List from it. 你第一次从中得到一个List。

So, fetch a List, check if it's null. 因此,获取一个List,检查它是否为null。

If it's null, make a new list, add the string to it, put the List back. 如果它为null,则创建一个新列表,将字符串添加到其中,然后将List放回。 If it's anything but null, add to it, or do what you want. 如果它只是null,添加到它,或做你想要的。

You forgot to mention the class. 你忘了提这堂课。 Map here is the reference type and is an Interface . 这里的Map是引用类型,是一个接口 HashMap on the other side of equals specifies the actual type of the Object created and assigned to the reference foo . equals另一侧的HashMap指定创建并分配给引用fooObject实际类型。

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

The actual type specified ( HashMap here) must be assignable to the reference type ( Map here) ie if the type of reference is an Interface , the Object's type must implement it. 指定的实际类型(此处为HashMap )必须可分配给引用类型(此处为Map ),即如果引用类型是接口 ,则Object的类型必须实现它。 And, if the type of the reference is a Class , the Object's type must either be the same class or its subtype ie it extends from it. 并且,如果引用的类型是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 not recommended. 推荐您的第二种实例化方法。 Stick to using List which is an Interface . 坚持使用List作为接口

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

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

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. 等等

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

相关问题 Java转换列表 <String> 到地图 <String, String> - Java Convert List<String> to Map<String, String> 转换列表<String>到地图<String, String>在 Java 中 - Convert List<String> to Map<String, String> in Java 如何转换地图 <String, List<String> &gt;到地图 <String, String> 在java 8中 - How to Convert a Map<String, List<String>> to Map<String, String> in java 8 将 map 字符串转换为 map 字符串列表字符串 - Java 7 - convert map string string to map string list string - Java 7 比较列表<map<string, string> &gt; 到列表<map<string, object> &gt; Java </map<string,></map<string,> - Comparing a List<Map<String, String>> to a List<Map<String, Object>> Java Java 8列表 <Foo> 到地图 <String, Map<String, List<String> &gt;&gt; - Java 8 List<Foo> to Map<String, Map<String, List<String>>> 转换列表<HashMap<String,String> &gt; 列出<map<String,String> &gt; 爪哇 - convert List<HashMap<String,String>> to list<map<String,String>> java 有没有办法实现地图 <string, map<string, list<string> &gt;&gt;在protobuf 3中? - Is there a way to implement map<string, map<string, list<string>>> in protobuf 3? 映射 Java8 的字符串列表 - List of String to Map Java8 我应该如何实例化列表<List<String> &gt; 在 Java 中 - How should I instantiate List<List<String>> in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM