aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashcode and equals  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashcode and equals  " Watch "hashcode and equals  " New topic
Author

hashcode and equals

Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
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.


SCJP1.5(81%), SCDJWS(94%), next mission SCEA (but need to wait or that)
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

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





Thanks,


cmbhatt
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14156
    
  19

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
harinath chakrapani
Ranch Hand

Joined: Jun 11, 2007
Posts: 40
. 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.
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
Chandra in your code it is comparing two references, which refer to the same object.


SCJP-1.5<br />SCWCD-1.4
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Jun 18, 2007
Posts: 383
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Jun 18, 2007
Posts: 383
Thanks a lot Chandra. I got it now.

Thanks again.
sudha javvadi
Ranch Hand

Joined: Mar 23, 2007
Posts: 97
Very nice explanation chandra . I also have that same doubt. Now i got it. Thanks!
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
phani bhushan reddy
Greenhorn

Joined: Jul 20, 2007
Posts: 27

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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Jun 04, 2007
Posts: 377
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

Joined: Feb 28, 2007
Posts: 1707
[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,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashcode and equals