• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Using Generics in HashMap

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does this output?
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic