aspose file tools*
The moose likes Java in General and the fly likes Using Generics in HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using Generics in HashMap" Watch "Using Generics in HashMap" New topic
Author

Using Generics in HashMap

keshav pradeep ramanath
Greenhorn

Joined: Aug 18, 2011
Posts: 18
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
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

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

Joined: Mar 17, 2011
Posts: 8008
    
  22

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
keshav pradeep ramanath
Greenhorn

Joined: Aug 18, 2011
Posts: 18
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

Joined: May 03, 2008
Posts: 4642
    
    5

What does this output?


luck, db
There are no new questions, but there may be new answers.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

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

Joined: Apr 06, 2010
Posts: 4422
    
    8

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

Joined: May 03, 2008
Posts: 4642
    
    5

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

Joined: Aug 18, 2011
Posts: 18
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

Joined: Jan 10, 2002
Posts: 61426
    
  67

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.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
keshav pradeep ramanath
Greenhorn

Joined: Aug 18, 2011
Posts: 18
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

Joined: Mar 17, 2011
Posts: 8008
    
  22

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

Joined: May 03, 2008
Posts: 4642
    
    5

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?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using Generics in HashMap