8

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. Changing the above line by adding a cast removes the error. I looked at the related posts on stackoverflow before posting this question. Seems to be a strange issue.

Map<String, Integer> cache = (Map<String, Integer>) new HashMap();
4
  • 12
    Are you sure both Map and HashMap come from java.util package in your code? Commented Sep 24, 2013 at 14:51
  • @Shane - What is the error ? Are you using any IDE like eclipse ? Commented Sep 24, 2013 at 14:51
  • 3
    Are you declaring your own non-generic HashMap class anywhere? Please provide a short but complete program demonstrating the problem. Commented Sep 24, 2013 at 14:54
  • 1
    I was getting the same error. In my case, I had given my class the same name i.e created a class labeled HashMap. This was conflicting with the java.util.HasMap. make sure your class name is different Commented Feb 10, 2019 at 10:11

14 Answers 14

30

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

Sign up to request clarification or add additional context in comments.

Comments

8

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.

Comments

3

I'm sure that you are importing the wrong HashMap. You have to use the java.util packages for the code you presented there.

We can't help you any further without seeing your import statements.

Comments

2

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

2 Comments

what do you mean by HashMap is not generic. The definition of java.util.HashMap is: public class HashMap<K,V> extends AbstractMap<K,V>
Welcome to SO! Please read the tour, and How do I write a good answer?
0

If none of the above solution works ,the only possible reason you are getting this error is because you might have named your class_name similar to an already existing class which is either in util or lang library .

Comments

0

Recently, I have met the same problem.Almost I have found all the answers, but I failed.
I had imported java.util.HashMap and java.util.Map or set the JRE System Library to the top.But in vain.

At last, I tried to change my Class name from HashMap to HashMapDemo. Now, I just want to tell you that your Class name may be one of the java.util.*, you should change your Class name.

Comments

0

If you have named your class with the same name as other class you can always do the following:

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

It's a common mistake when you are trying a new language/library capability to call the class with the same name as the class you are trying, anyway this is not only useful to this purpose.

For example there are multiple classes name Date like Java.sql.Date and java.util.Date, in this case if you need to reference both it's useful to know that.

Comments

0

Never name or keep your class name same as any class which is predefined in Java Util.

For example: In Java HashMap is predefined class and if create a new class with same name i.e HashMap then it's obvious to get such error.

1 Comment

Welcome on SO. When answering to questions, try to adress the question, not other answers.
0

Try with this:

  1. Create a new class and give some another unique name in same package

  2. import - import java.util.*; or import java.util.HashMap;

  3. create HashMap as:

HashMap<Integer,String> map=new HashMap<Integer,String>(); (old verbose way)

or

HashMap<String, Integer> map = new HashMap<>(); (better way)

or

Map<String, Integer> map = new HashMap<>(); (best way)

compile and run

Comments

0

This can happen when the Java version you compile to is lower than your JDK. Make sure the "compiler compliance level", as Eclipse calls it, is high enough. This happened to me. The problem happened in Eclipse when my JDK was Java 7, but the compiler compliance level was at 1.4. On my Eclipse this setting is at Project->Properties->Java Compiler. I raised the compiler compliance level to 6.0 and the problem went away.

Comments

0

use java.util.Map.<String, Integer> cache = new HashMap<String, Integer>(); and import import java.util.*;

1 Comment

This answer does not bring anything new to the thread, similar solution was already proposed for example in this answer: stackoverflow.com/a/53969107/3157428
0
  • If your using vs code and you forgot to install vs code for java package first do that.
  • Change your class name to something different than the name present in your src folder. do not name it HashMap.java.
  • import java.util.HashMap; there you go it will fix the problem, it did for me.

Comments

0

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

This Error Occurs Due To Your Project File Name Check Your File And Change That.

And Import

import java.util.Map;

It Will Work For Me

Comments

-1

I did something really stupid to get this error. You might check. I named my class "HashMap" lol. You might check that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.