This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Implementing hashcode and equals method in a Map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Implementing hashcode and equals method in a Map" Watch "Implementing hashcode and equals method in a Map" New topic
Author

Implementing hashcode and equals method in a Map

Arun Sanker
Ranch Hand

Joined: Mar 21, 2007
Posts: 44
Hi Guys,

I have 2 kinds of map ,a HashMap and a HashTable - in which I store some userdefined objects,Say a "Color" object.

So my collection set will look as follows:

HashMap<Color,Color> objects(userdefined "Color" objects).
Hashtable<Color,Color> objects(userdefined "Color" objects).

For example:-

(color1,color20)
(color2,color21)
(color3,color22)
(color4,color23)
(color4,color24)
(color5,color25)


So,Now to my question.
In java documentation it says that

>>To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.

[1]How should I implement these "hashcode" and "equals" method in my Color object.

Class rgbColor implements Color{


}

[2]As my "key" and and "value" in the maps are both "Color" objects.Does it cause a problem?(I mean implementing the "equals" and "hashcode" method).


Would be great if some one could put some light into it?

Thanks
P
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3606
    
  15
Originally posted by Arun Sanker:

[2]As my "key" and and "value" in the maps are both "Color" objects.Does it cause a problem?(I mean implementing the "equals" and "hashcode" method).


Only the objects used as keys need to implement equals/hashcode. It doesn't matter whether or not the value objects do.

Just out of interest in what way are you using a Map that has the same object type for both key and value ?
[ November 27, 2008: Message edited by: Joanne Neal ]

Joanne
Arun Sanker
Ranch Hand

Joined: Mar 21, 2007
Posts: 44
My color objects are of type rgb,cmyk,hilightcolor.
so each object is a different color object.
[ November 28, 2008: Message edited by: Arun Sanker ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Search through this forum; how to implement hashCode and equals methods comes up every now and again.
The principles are the same, and are described in Object, here and here. If you get your hands on a copy of Effective Java by Joshua Bloch there is a chapter about that sort of methods.

To implement a hashCode, try the hashCode of the class name, then multiply by 31 and add the hashCode for your first field, then multiply by 31 and add . . .
Every field used in "equals" must be used for the hashCode too.

To implement equals, remember you might pass the same object, so if this == obj is true, you don't need to check any more.
You will have to check that the other object isn't null, that it is of the same class, then cast obj and that each of its fields is equal to the corresponding field of this.
You need the @Override annotation for both methods.
If you try it, you will find that I have written the instructions in that order for a good reason!
Arun Sanker
Ranch Hand

Joined: Mar 21, 2007
Posts: 44
Thanks Cambelll
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
I presume you got it to work well done.
 
GeeCON Prague 2014
 
subject: Implementing hashcode and equals method in a Map