It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Map : Serious Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Map : Serious Doubt" Watch "Map : Serious Doubt" New topic

Map : Serious Doubt

Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 392

This code compiles fine.
What does the bold line means? What type will the key and Value be of ?

I have seen mostly ;
Map<Key,Value> m = new HashMap<Key,Value>();

where both Key and Value are Objects.
[ October 28, 2006: Message edited by: Sandeep Vaid ]
Venkat Sidh
Ranch Hand

Joined: Sep 30, 2006
Posts: 61
Your code is referring to non-generic code so Key & Value be of any Objects. If you're planning to use Java object as a Key then you need to override both equals() and hashcode()
Costa lamona
Ranch Hand

Joined: Sep 24, 2006
Posts: 102
Note that

HashMap always contains two objects pair even with generics
Generics are cause "type ensurance" on compile time only.

Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi cowboys,

you all ran into the trap.


the bold line doesn't refer to the collections class Map, but to Sandeep's own class called Map....
And it can never compile. Unless you have also a class named HashMap in your package that extends your Map class.

: eek :


all events occur in real time
Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 392
Later i also realize that my class name is also Map.

Thanks you all . I got the answer that i wanted...
I agree. Here's the link:
subject: Map : Serious Doubt
It's not a secret anymore!