Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type HashMap is not generic; it cannot be parameterized with arguments <String, Integer>

Tags:

java

This is a strange error i am getting today when i try to implement a Map as below.

Map<String, Integer> cache = new HashMap<String, Integer>();

I am using JDK 1.7 and not sure why this error has been coming and changing the above line by adding cast removes the error. I looked at the related posts in stackoverflow before posting this question seems to be strange issue.

Map<String, Integer> cache = (Map<String, Integer>) new HashMap();
like image 743
Shane Avatar asked Sep 24 '13 14:09

Shane


People also ask

Why HashMap is not generic?

The type HashMap is not generic; it cannot be parameterized with arguments <String, Integer> If this type of error occur than first check your file name and class name Beause by mistake you save file name as HashMap.java Than try other ways what do you mean by HashMap is not generic.

Is it possible to extend HashMap with a custom interface?

We've got a class that Extends HashMap and then implements a custom Interface. Type safety: The method put (Object, Object) belongs to the raw type HashMap. References to generic type HashMap<K,V> should be parameterized

Is List A generic type in Java?

The type List is not generic; it cannot be parameterized with arguments. In Java, the java.util and java.awt packages both define a type called List. The util version is the collections interface that is commonly used. The awt version is a class used for creating a dropdown list GUI element.


2 Answers

Check you are actually using java.util.HashMap and java.util.Map in your imports.

like image 108
Alfredo Osorio Avatar answered Oct 04 '22 23:10

Alfredo Osorio


I have also gone through the same error but it was resolved just by changing some properties of the project:

  • Right-click on your project
  • Click on Properties
  • Select Java Build Path from right-hand side panel
  • Select Order and Export tab
  • Click on your JRE System Library or JDK Library
  • Click on Up button and move it to first position
  • Click Ok
  • Clean & build your project.

Do repeat this for all other dependents project as well, if you have dependencies.

It resolved my issue because previously the Java files were picking other libraries and packages not from JRE package as it was ordered set in last priority.

like image 35
Shwetank Rastogi Avatar answered Oct 05 '22 00:10

Shwetank Rastogi