• 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

Please anybody clear my doubts on hashcode

 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi to all
I have various doubts on hashcode() .Please clearify them separely in detail?

Question 1.Is it necessary to override the equals() & hashcode() methods in collection like HashSet,HashMap,LinkedHashMap etc. If not why and if yes then why?In what condition we have to override them?Please explain in detail?

Question 2.What happened when we do not override both (equals()& hashcode()) of them. Will it generate compiler error.If yes then why and if not then why?

Question 3. What happened if we override only equals() but not override the hashcode() ?

Question 4. What happened if we override only hashcode() ,not equals() method ?

Question 5.And please also explain me this below code,what is the effect of hashcode() in this code if we include this hashcode()or not.


The answer is 3 but i think it should be 2 because HashSet do not allow duplicates data.



import java.util.*;
class hashcodeoverriding
{
String s;
hashcodeoverriding(String p)
{
s=p;
}

public boolean equals(Object o)
{
if((o instanceof hashcodeoverriding) && (this .s==((hashcodeoverriding)o).s))
return true;
else return false;
}


public int hashcode()
{
//return (s.length());
return 5;
}

//public String toString()
//{
//return s;
//}



public static void main(String...args)
{
HashSet x=new HashSet();
x.add(new hashcodeoverriding("Ram"));
x.add(new hashcodeoverriding("Prad"));
x.add(new hashcodeoverriding("Prad"));


System.out.println(x.size());
}



}

 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These two links will help you out i guess-:

Overriding and Implementation of hashcode()

equals() and ==
 
pradeep singh
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
i am unable to get clearify my doubts.Please someone clear me.
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pradeep,
As from previous links you may have got what a equals() mehthod is and why we use it and how hashcode() is useful.
For more details on hashcode you may visit-:
HashCode
Hashtable

All your questions can be answered from the below excerpt form Effective Java-:

A common source of bugs is the failure to override the hashCode method. You must override hashCode in every class that overrides equals. Failure to do so will result in a violation of the general contract for Object.hashCode, which will prevent your class from functioning properly in conjunction with all hash-based collections,including HashMap, HashSet, and Hashtable.[/B

]

You can visit this link to read out more-:
effectivejava

[B]So to be truly safe, your rule of thumb should
be, if you override equals(), override hashCode() as well.
Kathy & Bates



Now your 5th Question, here is a small typo error as you have written hashcode instead of hashCode with a Capital C.

Due to this your code was not overriding the hashCode() method and giving irrelevant result. Here you can also get why overriding of these is necessary . Try to run the below code, i have checked the typo and got the desired result.



Try commenting equals() or hashCode() and again you won't get the expected results.
 
pradeep singh
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pranav Bhatt
Thanks a lottttttttttttt. Thank you for your this helping attitude.Now i am getting cleared more than before .

Please also tell me Is it(hashCode()) implicitely called and if yes then who called it.

And 2 new question is that in below question i have not override equals and hashCode () method but even then it does not allow duplicate i.e."a" twice BUT if i use commented line (by making commented line uncomment) then it allow duplicates of object i.e. new SetTest().How and Why it show different behaviour?



import java.util.*;

class SetTest
{ int i;
SetTest(int x)
{
i=x;
}
public static void main(String...args)
{

Set s=new HashSet();


s.add("a");
s.add(new Integer(42));
s.add("b");
s.add("a");
s.add(new Object());

//s.add(new SetTest(50));
//s.add(new SetTest(50));



for(Object o:s)
System.out.println(o +"");

}

}



[ January 14, 2008: Message edited by: pradeep singh ]
[ January 14, 2008: Message edited by: pradeep singh ]
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason that duplicates are not allowed in this example is because the objects that you are adding (well, the String and Integer) have over-ridden their equals() and hashCode() methods.

When we are talking about over-riding these methods it is usually because we have created our own classes and are going to be adding them to sets or maps - so those collections need to be able to make comparisons on these objects in order to position and locate them.
 
pradeep singh
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ian Edwards .
Please also tell me Is it(hashCode()) implicitely called and if yes then who called it?

Which other classes other than Integer and String ,are override the hashCode() method.
 
Ian Edwards
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please also tell me Is it(hashCode()) implicitely called and if yes then who called it?



If you are adding an object to a HashMap for instance, the HashMap add() method will call the hashCode() method on the object being added.

Which other classes other than Integer and String ,are override the hashCode() method.



I won't be able to answer this has there are probably hundreds of them through the whole API. I can tell you that the StringBuffer class doesn't override the equals or hashCode method.
 
pradeep singh
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanku very much Ian Edward.
Thanks again .
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Which other classes other than Integer and String ,are override the hashCode() method.



There may be others too as i dont know about all of them but all the Wrapper classes and String class override equals() and hashCode() in Object Class as far as i know
reply
    Bookmark Topic Watch Topic
  • New Topic