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(); 
+6
source share
7 answers

Make sure you really use java.util.HashMap and java.util.Map in Import .

+21
source

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 Path from the right panel
  • Select the Order and Export tab
  • Click on JRE System Library or JDK Library
  • Press the Up button 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.

+4
source

I am sure that you are importing the wrong HashMap . You should use java.util packages for the code you presented there.

We cannot help you without seeing your import operators.

+2
source

If none of the above solutions work, the only possible reason you get this error is that you could call your class name similar to an existing class, which is either in the utility or in the lang library.

0
source

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

0
source

Shane will kindly verify that you have a HashMap as the name of your class. Therefore, if you change the name of the java file, this error will no longer exist.

0
source

I did something really stupid to get this error. You can check. I called my class "HashMap" lol. You can check it out.

-1
source

Source: https://habr.com/ru/post/954516/


All Articles