Java 不能实例化地图...为什么不呢?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19598369/
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
Can't Instantiate Map...well why not?
提问by PinkElephantsOnParade
Map<String, ArrayList<Pair<String, Integer>>> k = new Map<String, ArrayList<Pair<String, Integer>>>();
This line is in my code. I'd like to instantiate a Map that contains a String then an ArrayList of Pairs of Strings and Integers.
这一行在我的代码中。我想实例化一个 Map,它包含一个字符串,然后是一个字符串和整数对的 ArrayList。
Pair is a class that I wrote that is in my package.
Pair 是我在包中编写的一个类。
I get "Cannot Instantiate the type Map>>();
我得到“无法实例化类型 Map>>();
Why not? Seems reasonable to me...
为什么不?对我来说似乎很合理...
采纳答案by rgettman
The built-in Map
is an interface, which cannot be instantiated. You can choose between lots of implementing concrete classes on the right side of your assignment, such as:
内置Map
是一个接口,不能实例化。您可以在作业右侧的许多实现具体类之间进行选择,例如:
ConcurrentHashMap
HashMap
LinkedHashMap
TreeMap
ConcurrentHashMap
HashMap
LinkedHashMap
TreeMap
and many others. The Javadocs for Map
lists many direct concrete implementations.
和许多其他人。该Javadoc文档Map
列出了许多直接的具体实现。
回答by Hussain Akhtar Wahid 'Ghouri'
Interfaces cant be intantiated You need to use some concrete class implementing the interface Try something like this
接口不能被初始化你需要使用一些实现接口的具体类试试这样的
Map<String, ArrayList<Pair<String, Integer>>> k = new HashMap<String, ArrayList<Pair<String, Integer>>>();