• 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() implementation

 
Ranch Hand
Posts: 37
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In SCJP Sun Certified Programmer for Java 6 Study Guide by Katherine Sierra and Bert Bates (K&B):


Chapter 7: Page number 553:
this one-hash-fits-all method would be considered appropiate and even correct because it does not violate the contract





In above statement, two words are used one is appropiate and and second is correct which looks like, both have different meanings. The second one is explained by saying because it does not violate the contract. Could you please explain the first word which is appropiate for above implementation?

What is the difference between appropiate implementation of hashCode() method and correct implementation of hashCode() method?

Thanks......

Matloob


 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Matloob,
I am NOT so sure about the best way to answer your question because "it does NOT violate the hashcode contract" is an 'appropriate answer'.

this: implementation simply puts all your elements in one bucket that has the 'ID=1492'...now java says that this implimentation does NOT violate the hashcode contract, hence it is "appropriate AND correct", but programmers should be aware that it is NOT recommendable.

I hope you understand.

Regards

Ikpefua.
 
Matloob Hussain
Ranch Hand
Posts: 37
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ikpefua,

Thank you for your reply. I got your your point.

Does the implementation of hashCode() method depends on the implementation of its equals() method? I means, when we are checking the appropiate implementation of hashCode() method, do we need to follow its equals() method as well?


Matloob
 
Ranch Hand
Posts: 144
Oracle Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matloob Hussain wrote:
when we are checking the appropiate implementation of hashCode() method, do we need to follow its equals() method as well?



If you look at the chart on the following page (544), the only required action are
1) Objects that are considered equal must have the same hashCode()
2) Objects with different hashCodes must not be considered equal

The reason the example hashCode work because it fulfills the Criteria 1 that every object will have the same hashCode. Criteria 2 is moot since it is impossible for the Object to return different hashCodes. The real issue you need to look for is when the hashCode starts dealing with instance variables.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) The implementation of hashCode() is not dependent on equals(). Only constraint is that it should be consistent.
2) The implementation of equals() is indirectly dependent on hashCode(), as the set of objects chosen for equals() method are selected from a single hashCode() bucket.

Thanks,
Thiyaneshwaran S
 
Matloob Hussain
Ranch Hand
Posts: 37
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thiyanesh,

Thank you for your reply.

So It means there could be two ways when we writing hashCode() and equals() methods.

1- write equals() method first in which decide which objects are going to be equals and then write hashCode() method to put them in same bucket.

2- write hashCode() method first assuming all the object going to be in one bucket and then write equals() method for same objects. Once done then impove hashing algorithm to put different objects in seperate bucket.


which way we should follow or is there any other way?

Thanks.

Matloob
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the two objects are equals then their Hashcode should be equals,
So we have to write first equals() methods and then check if the two objects are equals if they are equals then use hashcode method to put them in same bucket...

Thanks
Shanky
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys,
I noticed that one of the MOST efficient ways to make analysis in java is to 'see-things-in-action' so I have written a small program that uses hashing, I appeal that we analyze the program together:



output:
true
true
false
2
________________________________________________________________________________

1. First of all we know that 'Sets' should NOT allow duplicates
2. In other for Set<Candles> s = new HashSet<Candles>(); NOT to
allow duplicates, the hashCode() and equals() methods of the Candle
class must be overridden according to the hashCode and equals contract.
In this case we have used a very effective strategy (the instance variable quantity).

My theory:
The following takes place when the add() method is invoked;

If the hashCode() returns 'true' and the equals returns 'true' NO element is added
If the hashCode() returns 'true' and the equals returns 'false' the element is added
If the hashCode() returns 'false' (happens when you dont override or when there is
no exisiting one) a new bucket is allocated, in this case equals() value is NOT
effective because there are NO elements in a new bucket to 'compare' so the
element is added.

One question comes to mind...Why this behaviour? answer... the Set's add() method
has been implemented by java to behave this way, in other for duplicates NOT to be added.

@Matloob study the program, make modifications(example, erase or comment
the hashCode() and equals() method) and see what happens to the output.
Like this we will clarify all the doubts.
 
S Thiyanesh
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The hashCode() method implementation should make sure
1) to have same hash code value for identical objects(this is mandatory) and
2) try to have different hash code value for non-identical objects(though this is not mandatory).
By identical, i mean two objects having same state(identified by any key parameter in the object) and these two identical objects should have the same hashcode value. Still there is a possibility of non-identical objects having same hashCode() and its allowed legally.

hashCode() is useful in identifying the set of objects that fall under a common hash value.

Till now we are not worried about comparing two objects.

Now assume we have an object and we need to find whether the object exists in a collection.
Now we need to override the equals() method to define the identify the object. Only the set of objects that has the same hashcode value as that of the object under check will used for comparison in the equals method.
Out these objects that have the same hashcode value as the object under consideration, equal method helps in identifying the exact object.(based on the state of the object)

General rule is
1) equals() method should 100% determine whether two objects are equal(this could be two reference variables pointing to same object or two reference variables pointing to different object having the same state)
2) hashCode() method should make sure the similar objects always fall under the same bucket. (Still different objects can fall under the same bucket, but then number of comparisons made by equals method will increase. Hence we should strive to make unique hashcode value for different objects.)


 
Matloob Hussain
Ranch Hand
Posts: 37
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

The code example given by Ikpefua is showing to me that the equals() method was written before hashCode. Is that right? Ikpefua.

Shanky wrote:
we have to write first equals() methods and then check if the two objects are equals if they are equals then use hashcode method to put them in same bucket...



I understand it and agree with it as well. Thanks........ shanky.

Again,the code example given by Ikpefua, explains to me how these two method works in HashSet.


boolean add(Object o) method behaves as:

if(hashCode() == false){ // could not find the bucket
create new bucket;
add the element;
return true;
}
else{
if(equals() == false) // could not match the object in bucket
add element;
return true;
else{
return false; // match the object from the bucket, return without adding element because set does not allow duplicates....
}

}

boolean remove(Object o) method behave as:
if(hashCode() == false) // could not find the bucket
return false;
else{
if(equals() == false) // could not match the object in bucket
return false;
else{
remove element; // match the object from the bucket and delete it
return true;
}
}
 
Honk if you love justice! And honk twice for tiny ads!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic