• 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

equal() & hashcode()

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic