The HashMap type is not generic; it cannot be parameterized with the arguments <String, Integer>
This is a strange error that I get today when I try to implement a map as shown below.
Map<String, Integer> cache = new HashMap<String, Integer>(); I am using JDK 1.7 and donβt know why this error appeared and changing the above line, adding a throw, removes the error. I looked at related posts in stackoverflow before posting this question, it seems like a strange problem.
Map<String, Integer> cache = (Map<String, Integer>) new HashMap(); I also experienced the same error, but this was resolved by simply changing some project properties:
- Right click on your project
- Click
Properties - Choose
Java Build Pathfrom the right panel - Select the
Order and Exporttab - Click on
JRE System Library or JDK Library - Press the
Upbutton and move it to the first position. - Click
Ok - Clean and create your project.
Repeat this for all other dependency projects if you have addictions.
It solved my problem because earlier Java files were compiling other libraries and packages not from the JRE package, since it was ordered in the last priority.
@Neeraj Pandey, you definitely understood this and offered the absolutely correct answer.
Never call your class name the same as any class that is predefined in Java Util. For example: in Java, HashMap is a predefined class, and if you create a new class with the same name, that is, HashMap, it will become obvious to get such an error.
Therefore, avoid making such mistakes.