aspose file tools*
The moose likes Java in General and the fly likes default behaviour of hashcode() in object class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "default behaviour of hashcode() in object class" Watch "default behaviour of hashcode() in object class" New topic
Author

default behaviour of hashcode() in object class

Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Hi Guys,
I just want to know what the default behavior of hashcode() in object class..?As when we define our own user defined collection we overwrite hashcode() to store the object in a particular bucket and to make searching faster..??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

Saral Saxena wrote:
I just want to know what the default behavior of hashcode() in object class..?As when we define our own user defined collection we overwrite hashcode() to store the object in a particular bucket and to make searching faster..??




The default behavior of the Object class is to create a unique value (or try as much as possible to) for the hash code. This is done because the equals() method of the Object class declares equality only if two instances are the exact same object.

Since your class would probably like a different version of equality, you'll need to change the hashCode() method to match. You don't need to override hashCode() for speed, you need to override it so that you honor the equals()/hashCode() contract (if you override equals()).

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202




The default behavior of the Object class is to create a unique value (or try as much as possible to) for the hash code. This is done because the equals() method of the Object class declares equality only if two instances are the exact same object.
Since your class would probably like a different version of equality, you'll need to change the hashCode() method to match. You don't need to override hashCode() for speed, you need to override it so that you honor the equals()/hashCode() contract (if you override equals()).




Hi Henry ,

Thanks a lot ...so you mean to say that by default object class hashcode() is comparing the object equality on object references and in our custom defined collection we want to compare the object on the basis of content and for that we have to override the hashcode() in our custom collection pojo..??
Siva Vulchi
Ranch Hand

Joined: Nov 17, 2011
Posts: 30
By default hashcode() of object class returns integer equivalent of object's internal memory address. So if we dont override hashcode() for collections such as HashMap then each object will have unique hashcode which does not make search faster and hence sole intention of hashing mechanism has been not utilized.

So we should have to override hashcode() in such a way that some of the unequal objects(HashMap keys) return same hashcode.
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Siva Vulchi wrote:By default hashcode() of object class returns integer equivalent of object's internal memory address. So if we dont override hashcode() for collections such as HashMap then each object will have unique hashcode which does not make search faster and hence sole intention of hashing mechanism has been not utilized.

So we should have to override hashcode() in such a way that some of the unequal objects(HashMap keys) return same hashcode.


Hi Siva Vulchi,

So we override the hashcode() of object class so that if two object contain same content then their hashcose must be same and in that case duplicates should not be accepted as we do in set , in which if we pass the two keys having the same string value in that case duplicate is not accepted since string class have override the hashcode() of object class..!!and also please explain how by overridding the hashcode() make search faster..!!!

What I have my understanding is that ..the hashcodes are generated from the address of the object, so the hashcode for a key is entirely dependent on where it is stored in memory.You must override the default hashCode() method so that the hashcodes for keys are produced from the data members of the key objects.This ensures that the hashcode for a given key is always the same..!!
Anirudh Jhinaa
Greenhorn

Joined: Aug 25, 2007
Posts: 8
I am not sure if I understand where your confusion is. But I will try explaining the concepts:

Overriding the equals() method from Object class ensures that the object of this class can be used as elements in Collections. e.g.
Collections have contains() and containsAll() method which internally uses equals() on each element to determine whether the element is present in that collection or not. The same is true if you were to find an element in a collection by iterating over it.

Overriding hashCode() ensures that the object of the overriding class can be used as elements in Collections whose implementation uses hashing for storing/retrieving these elements.
The default implementation of hashCode() in the Object class returns the integer representation of memory location for the object.

There are certain rules that should be followed while implementing equals() and hashCode().

For equals() implementation the properties of equivalence relations should be satisfied: i.e. Reflexive, Symmetrical, Transitive, Consistent, and comparison with null. I would not get into details, kindly look up for them

Similarly for implementing hashCode() there are certain rules that needs to be understood, one of them being that if two objects are equal, they should always return same hash value.
Reason is that buckets for storing\retrieving these elements are determined based on the hash value.
Though un-equal object too can have the same hash value. This is called as collision. In such cases the equals() is used to search for the desired element.
As Henry mentioned that one should override hashCode() if equals() is being overridden.
Efficient hash functions will distribute the objects uniformly. There are various heuristics that can be used to have an efficient hash function. Again am not getting into details here.


Sheela Khanna wrote: You must override the default hashCode() method so that the hashcodes for keys are produced from the data members of the key objects.This ensures that the hashcode for a given key is always the same..!!

The data members that are used should be significant, and having redundant members does not help. Also one should take care that this does not break the contract with equal() method (i.e. the significant data members used in the equals() implementation should be used in hashCode()).


I hope things are clear now.

Anirudh
Siva Vulchi
Ranch Hand

Joined: Nov 17, 2011
Posts: 30
Hi Sheela,

Suppose we have HashMap<String,Object>. If we add 2 entries (key1,object1) and (key2,object2) where key1 & key2 hashcodes are same then these will be placed in linkedlist of same hash bucket. In HashMap each hash bucket is associated with a LinkedList object which stores all entries with keys of same hashcode. While searching for entry with key as key1, first it finds hashbucket then gets linkedlist and iterates it until it gets entry with key equal to key1 using equals() method and gets the corresponding object1.


Search is faster in the collections which use hashing mechanism. The reason is that... Take a list and map objects having 10 entries. Just assume that entries are added as followed.
list - 1,2,3,4,5,6,7,8,9,10
map - [2],[1,5,7],[3,6,9],[4,8,10]

Here map has 4 hashbuckets and each bucket holds entries with same hashcode.

If we want to search 6 th element..then in list it has to iterate 6 elements where it has to compare the input element with 6 elements.
Where as incase of HashMap, first it finds the 3rd hashbucket with hashcode then it gets linkedlist and starts to iterate until it gets the match. So here it has to compare only 2 elements. Overall operations required to perform are less compared to list.

This is my understanding. Hope it may help.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7795
    
  21

Siva Vulchi wrote:By default hashcode() of object class returns integer equivalent of object's internal memory address.

Actually, that may be true, but it's not required. This from java.lang.Object API docs:
"(hashCode()) is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language."

And you should never write a program that relies on it being done that way.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

I havent been able to read the complete discussion. But when it comes to overriding hashCode or equals methods you have to be careful and the implementations should follow rules laid down by Java Language Specification.
Effective Java by Joshua Bloch has really detailed article on these concepts, so that would be an useful read.


Mohamed Sanaulla | My Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: default behaviour of hashcode() in object class