Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 758
PinkElephantsOnParade Avatar asked Oct 25 '13 19:10

PinkElephantsOnParade


2 Answers

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.

like image 134
rgettman Avatar answered Nov 20 '22 10:11

rgettman


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>>>();
like image 38
Hussain Akhtar Wahid 'Ghouri' Avatar answered Nov 20 '22 09:11

Hussain Akhtar Wahid 'Ghouri'