File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implementing hashcode and equals method in a Map

 
Arun Sanker
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Arun Sanker
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Cambelll
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you got it to work well done.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic