简体   繁体   中英

Can't Instantiate Map…well why not?

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.

Pair is a class that I wrote that is in my package.

I get "Cannot Instantiate the type Map>>();

Why not? Seems reasonable to me...

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:

  • ConcurrentHashMap
  • HashMap
  • LinkedHashMap
  • TreeMap

and many others. The Javadocs for Map lists many direct concrete implementations.

接口无法实例化您需要使用一些实现接口的具体类尝试这样的事情

Map<String, ArrayList<Pair<String, Integer>>> k = new  HashMap<String, ArrayList<Pair<String, Integer>>>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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