• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Implementing hashcode and equals method in a Map

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Cambelll
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I presume you got it to work well done.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic