aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equal() & hashcode() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equal() & hashcode()" Watch "equal() & hashcode()" New topic
Author

equal() & hashcode()

dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
The following example is from K & B book,chapter:7,page:563.
This example is about equals & hashcode implementation. I am confused about bold text. I got first 2 lines of bold text. But in 3 & 4 line the hashcode succeeds (arthur & clover), but equals does not succeeds. Then after it gives output "Dog key". As I understand hashcode finds right bucket in this example so it gives the value which is in the bucket("Dog key"). So here what equals is doing? May be I am making you guys confuse. But can anyone explain?


SCJP-1.5<br />SCWCD-1.4
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424


In line 1 you changed name to "clover1" so step 1 of finding the right bucket will fail because of hashCode mismatch.

In line 2 you changed name to "arthur" so step 1 of finding the right bucket will succeed.. Here what you are missing.. name has become "arthur" in line 2, so equals() succeed.
[ October 11, 2007: Message edited by: Ahmed Yehia ]
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
The whole example is like below. Now can you explain?



If they're added to the end of MapTest.main():
d1.name = "magnolia";
System.out.println(m.get(d1)); // #1
d1.name = "clover";
System.out.println(m.get(new Dog("clover"))); // #2
d1.name = "arthur";
System.out.println(m.get(new Dog("clover"))); // #3
Remember that the hashcode is equal to the length of the name variable. When
you study a problem like this, it can be useful to think of the two stages of retrieval:
1. Use the hashcode() method to find the correct bucket
2. Use the equals() method to find the object in the bucket
In the first call to get(), the hashcode is 8 (magnolia) and it should be 6
(clover), so the retrieval fails at step 1 and we get null. In the second call to get(), the hashcodes are both 6, so step 1 succeeds. Once in the correct bucket (the "length of name = 6" bucket), the equals() method is invoked, and since Dog's equals() method compares names, equals() succeeds, and the output is Dog key. In the third invocation of get(), the hashcode test succeeds, but the equals() test fails because arthur is NOT equal to clover. (What happen in third case, they have not written the answer?)
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424

In this case, in line 1 you are setting the name of the object in the map to "arthur" then you are using a new Dog object (clover) to get the object which must match the object in the map.

First step of finding the correct bucket succeeds because both objects have the same hashCode.
Second step of comparing objects using equals() will fail, because "arthur" != "clover"
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
d1.name="arthur";
System.out.println(m.get(d1));//out put Dog key

d1.name = "arthur"; //1
System.out.println(m.get(new Dog2("clover"))); // output null

What is the difference in between this two code? Please explain me?
greg buela
Ranch Hand

Joined: Sep 04, 2007
Posts: 71

d1.name="arthur";
System.out.println(m.get(d1));//out put Dog key

d1.name = "arthur"; //1
System.out.println(m.get(new Dog2("clover"))); // output null

What is the difference in between this two code? Please explain me?


If you run those sentences in that order, the last one outputs null because you have already changed d1.name. By that point, for the element "Dog key", the instance of Dog2 that maps to it has the name "arthur". So, the new Dog2("clover") will find the right bucket but will fail to find the name, as per the definition of equals() in Dog2.


SCJP 1.5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equal() & hashcode()