• 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

hashcode and equals

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
5. What two statements are true about properly overridden hashCode() and equals()
methods?
A. hashCode() doesn�t have to be overridden if equals() is.
B. equals() doesn�t have to be overridden if hashCode() is.
C. hashCode() can always return the same value, regardless of the object that invoked it.
D. If two different objects that are not meaningfully equivalent both invoke hashCode(),
then hashCode() can�t return the same value for both invocations.
E. equals() can be true even if it�s comparing different objects.

The answer of above question is C and E , I didnt understand howcome E
Can anyone elaborate on this.
Thanks in Advance.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


E. equals() can be true even if it�s comparing different objects.






Thanks,
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To answer this question, it is a good idea to look at the Java API documentation of class Object and carefully read what it says about the equals() and hashCode() methods.

The equals() method checks if two objects are 'equal', which means the objects have equivalent values. It doesn't mean the two objects must be the exact same object.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. What two statements are true about properly overridden hashCode() and equals()
methods?
A. hashCode() doesn�t have to be overridden if equals() is.
B. equals() doesn�t have to be overridden if hashCode() is.
C. hashCode() can always return the same value, regardless of the object that invoked it.
D. If two different objects that are not meaningfully equivalent both invoke hashCode(),
then hashCode() can�t return the same value for both invocations.
E. equals() can be true even if it�s comparing different objects.

Hi,
hashCode is to allot the records by comparing keys i.e., it checks and finds which bucket to put the value. but values are checked for repetition(set does not allow repetition) by comparing object values in equals method. please check the hashCode and equals contract from k&b.

In short hashCode is used for object inequality and equals for object equality.
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chandra in your code it is comparing two references, which refer to the same object.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by harinath chakrapani:
In short hashCode is used for object inequality


Not correct, since two objects that are not equal may have the same hash code.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dolly shah:
Chandra in your code it is comparing two references, which refer to the same object.



NO!



Both ref variables are referring to two distinct objects. But your equals(...) method says they are meaningfully equal. It is flaw of
that equal() method that I tried to show you. Both the employee objects
have same name, but different empno. equals() method is saying two
objects equal on behalf of the name only.


Thanks,
[ June 29, 2007: Message edited by: Chandra Bhatt ]
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra. I got it.
I am reading K & B ,collection chapter. I have doubt on overriding equals() method. Below sentence is written in book.

"If you dont override equals() method, you wont be able to use those objects as a key in a hashtable & you probably wont get accurate sets."
I am not getting it. Can you explain?
Thanks.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dolly,


"If you dont override equals() method, you wont be able to use those objects as a key in a hashtable & you probably wont get accurate sets."
I am not getting it. Can you explain?



Hashtable, HashMap, etc use hashCode() and equals() method both for putting and retrieving key and object pair in so called buckets (we call it to
understand).

hashCode is used to decide the bucket and equals method is used to pick
the required object from that bucket as you know there may be more than
one object in that bucket.

If you don't override the hashCode() method, default implementation of
the hashCode that comes from the Object class will be used. Now see the scenario:



Hashtable uses hashCode to decide which bucket to put the object into,
suppose the generated hashCode for the Person key p1 is 20; Object saved!
Now you want to retrieve the object from the Hashtable, you used the same
reference variable of Person object you used to save the object; Hashtable
calculates the hashCode and finds it 20, picks the bucket and uses equals
method to get the object from the objects pool (there may be more
than one object in that bucket), selects the right one.

Next you created another Person. This object is same as the previous one
because you see the no passed to its constructor. Now when you tried
to retrieve the object from the Hashtable. hashCode is calculated,
and this time the hashCode may be different, because it is coming from
the default implementation of hashCode. Suppose this time hashCode comes
30. Bucket is searched, NOT found.

That is why it is must to override the hashCode and equals method for
the class you need to use objects as key of. So that each time you
ask for hashCode for an object it must be same. And equals method to
decide the right object from the many objects that may be lying inside
the bucket.


Thanks,
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Chandra. I got it now.

Thanks again.
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very nice explanation chandra . I also have that same doubt. Now i got it. Thanks!
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

perhaps try also to take Chandra's code and just add a properly overridden hashCode method. It still does not have an appropriate equals method:


Can you guess what it does now, with a hashCode but without an equals?
What will happen for the oppsite(equals method right, hashCode wrong or not overridden) ?


Yours,
Bu.
 
Greenhorn
Posts: 27
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To your question with a hash code method but without equals() method will allow duplicate entries.
But with equals() without a hash code method will be possible, it may hit under the performance conditions because whenever we try to insert an element it will place in a random allocation which is provided by default hash code method provided by object class. so, whenever to retrieve this would become a performance problem

If its not right please reply me , that what happens in that situation
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Phani Bhusan
But with equals() without a hash code method will be possible...

Before you consider any performance issue, you see whether you are able to
retrieve the object from the Map. Without overriding hashCode() method you
will be using default implementation that comes from the Object class.
Distinct hashCode for objects, even your equal method says they are
meaningfully equivalent. The search will fail in first attempt, because no
matching hashCode.

You talk about performance issue, saying your hashCode should be distributed
to the objects uniquely, that means in a bucket single object is placed.
This is not the ultimate requirement but as much uniquely you distribute
the hashCode to the objects as much better the performance would be.
It means find the bucket, pick the object from that, without going through
long object matching in the bucket (in case more than one objects are
there in that bucket because they are also given the same hashCode).


Thanks,
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by phani bhushan reddy:
But with equals() without a hash code method will be possible

Not korrekt. You may get duplicates.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Manfred]Not korrekt. You may get duplicates.

IMHO, you will get null, instead of duplicate. Searching is two step
process, find the correct bucket and then do the sequential (or whatever)
search to find the correct object from that bucket (applying equals(...) method).


In case, hashCode() is not overridden, the first step may fail.

Thanks,
 
Arthur, where are your pants? Check under this tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic