Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Generics in HashMap

 
keshav pradeep ramanath
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,
I just stated coding on HashMap,but have encountered an error while trying to create one.

Here is the piece of code i was trying to compile.

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

I also tried

HashMap<String, Integer> myMap = new HashMap<String, Integer>();

Both of these are throwing errors saying "The type HashMap is not generic;it cannot be parameterized with arguments <String,Integer>".

Can someone help here as i am unable to know why this kind of error it is throwing(I am using Eclipse).

I have JRE 7 installed in my system.

Thanks in advance


 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
keshav pradeep ramanath wrote:
I have JRE 7 installed in my system.


Both compile for me using JDK1.7.0_25 but I'm a little worried by you saying you have JRE 7 and not JDK 7 . You need JDK 7.

I can't remember when HashMap was 'generic' enabled; I thought it was from day 1 of generics so the error message is peculiar!
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
keshav pradeep ramanath wrote:Both of these are throwing errors saying "The type HashMap is not generic;it cannot be parameterized with arguments <String,Integer>".

That seems doubly odd since, like Richard, I'm pretty sure HashMap was "genericized" (ugh) from the moment generics was added (1.5).

Also: the message would seem to suggest that the compiler actually knows about generics; otherwise, on an older version of the SDK I'm pretty sure you'd just get an "invalid token" message.

All of which leads me to wonder: is this HashMap you're using actually java.util.HashMap?

Winston
 
keshav pradeep ramanath
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Richard,
I have jdk1.6.0_24 installed in my system.So i am not sure whats actually causing this error.

Hi Winston,
I am using "import java.util.HashMap;".It is indeed surprising to see that message.

But here if i tried to just use "Map mMap = new HashMap();",without using generics,its compiling fine.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does this output?
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see why it should fail using JDK 1.6 but in your position I would de-install JDK 1.6 and install JDK 1.7 .
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definitely weird. It sounds like it's a post-1.5 compiler with a pre-1.5 standard library. Is there any chance you've got another JDK or JRE on the classpath, and the class definition is being taken from there?
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
keshav pradeep ramanath wrote:"The type HashMap is not generic;it cannot be parameterized with arguments <String,Integer>".

... (I am using Eclipse).


I get exactly that hover text for "HashMap" in Eclipse when the Project -> Properties -> Java Compiler -> Compiler compliance level is anything earlier than 5.0.

The hover text for "<String, Integer>" is "Syntax error, parameterized types are only available if source level is 5.0"
 
keshav pradeep ramanath
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Darryl,
The output for System.out.println(new HashMap().getClass()); is "class main.HashMap"

Hi Richard,
Thanks for the advice.Would try installing JDK 1.7

Hi Matthew,
Yup,I have 2 JDKs in my Java folder .So need to remove the older one? Or 2 versions of JDK can cause this kind of problems?

Please help!!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64828
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
keshav pradeep ramanath wrote:The output for System.out.println(new HashMap().getClass()); is "class main.HashMap"

There's the problem. You're using someone else's HashMap class, not the standard one which is in java.util.
 
keshav pradeep ramanath
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,
In my program,i have put several classes under "main" package.
So i believe its that main it is referring to.

So is the problem something else here? Maybe having 2 JDKs is creating a problem?
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
keshav pradeep ramanath wrote:So i believe its that main it is referring to.

No, I don't think so. It sounds to me that if indeed you have a class called 'main' (which is wrong; class names should start with CAPITAL letters), you've defined a class called HashMap inside it. Alternatively, you have a package called 'main' that defines a HashMap class.

Winston
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
keshav pradeep ramanath wrote:In my program,i have put several classes under "main" package.

fine, but why do you have a class of your own named HashMap in that package?
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic