This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
I have read some topics relevant to Hashcode but still i am not getting the basic idea behind the method Hashcode,i know it returns an integer value....but still if anyone could explain me it will be realy kinda of you....
1)What is Hashcode?
2)What is the use of HashCode?actualy can say why we use hashcode in our program?
Hashcode is a technique used to improve the search or an object (and obviously unce you have found you can modify or delete or just verify if it exist).
As you already understood the basic idea is that each object could be identified from a unique integer.
Lets make it easy. You hava a collection of Dog class instances with three properties:
You already (must) know that to compare one object with another you can override the equals(Object a) method.
So for the Dog you could define that dogFirst.equals(dogSecond) == TRUE
If they have the same name and the same owner, right?
Now suppose that you have a smart function (ok..ok... it is our hash function) that generate a different number for each two unique pair (name, owner).
In this case doing dogFirst.hashcode == dogSecond.hashcode
should give the same result than the equal method.
Thats the basic idea. After this the things could became complex (the hashfunction is not so easy to write); but the basic idea is this: you have a very complicated object but if you can hash it you can uniquely identifying in the middle of thousand of (same kind) of objects and you can easily create an index of int without looking everytime inside its properties.
ciao Maurizio
dhwani mathur
Ranch Hand
Joined: May 08, 2007
Posts: 621
posted
0
Thanks a lot for such a great explanation Maurizio Nagni
,now i am clear with hashcode,but still have some doubt if you could help me?
As you said it is used to compare objects,since each object is now identified with a particular integer,in case of wrapper classes value of equals method is TRUE bcoz equals method is overriden in wrapper class....but what if i try to compare objects of user class say A it will return false since i have to override the equals method.. but why it is said that if we want to override equals method we have to write hashcode method for it?
what i think is we have to write hashcode so as to assign integer value to objects isnt it?
but what the use of this integer value in equals method as it returns boolean value
if you can explain it will be realy helpful....
Thanks in advance............
Preparing SCJP 1.5
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
posted
0
[dhwani mathur] why it is said that if we want to override equals method we have to write hashcode method for it?
The use of hashCode is to improve the performance in placing and retrieving objects from the collection. It is ok to override equals(Object o) method for your class so that you could compare two objects whether they are meaningfully equal or not.
What if you use object of your class as a key to Map. Storing or retrieving the object from the Map is two step process. We imagine the use of hashCode as determining which bucket to place the object in. Your hashCode tells that. As the unique distribution of hashCode is, as much efficient and fast your search process will be. Before an object corresponding to the key is stored in the map, hashCode is computed to find the bucket to place the object in.
Let us talk about retrieving the object from the bucket given the key that is object of your class. Again hashCode is computed, and bucket is found, now the role of equals() method start comparing whether the object that is in the bucket is the one that you are looking for. If not search fails.
If there are more than one object in the map, (that is the case when more than one object's hashCode are same therefore they had been assigned the same bucket), now equals() starts the comparison in sequential order (not known, it may apply any other method also, not of our concern).
You think, even if there is no unique distribution of the hashCode to the objects, our search is limited to the given bucket instead of going through all the objects in the Map and applying equals() method to compare two objects.
hashCode is there to improve performance. If you don't override the hashCode() method for the class, the objects you are using as key to the Map, the default implementation of the hashCode works. Let's see the scenario:
Line #1: Why the second get() method returned null, we see both object emp1 and emp2 are meaningfully equal because they are having same "Amit", then what heck went wrong in retrieval of object from the Map using another same object reference emp2.
Reason : That is because Map is using hashCode() version of Object class. You may try this:
You will see the hashCodes for both are different. So the search process fails at first round, before our equals() method could work. That is why it is said you override the hashCode also. After you override the hashCode, you should see that emp1.hashCode() and emp2.hashCode() return the same value. It should be like that, I mean equals() and hashCode() contract.
Thanks,
cmbhatt
Maurizio Nagni
Ranch Hand
Joined: May 29, 2004
Posts: 75
posted
0
it is always a pleasure when people took so much of their time to help the others. thanks Chandra
dhwani mathur
Ranch Hand
Joined: May 08, 2007
Posts: 621
posted
0
Hi!!Chandra
Superb !!!Explanation I am now clear with the concept. Thanks a lot for helping me soo much.
vin Hari
Ranch Hand
Joined: Nov 16, 2006
Posts: 161
posted
0
Hi chandra, I saw your answers it was very good explanation my problem is also something related to this iam creating menu driven program for some keys iam getting there values for some it is null even i tried to write overriding equals and hashcode it did not work have a patience and check this code and please reply me This class does not contain any main method because iam calling from other class.please help me.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.