• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

K & B - Generics & Collections

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading through this chapter and observed the following code on page 565,



according to book, the answers to above code are

#1 fails
#2 succeeds
#3 fails

to my understanding #3 should also succeed because new Dog object is passed but not m.get(d1)

Correct me if I'm wrong, sorry you need to have K & B to know actual code of the above question.

Thanks.
[ October 18, 2006: Message edited by: Venkat Sidh ]
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
#3 fails because you change the d1.name to arthur before getting the value from the map. so hash code is calculated for the string arthur. but then you try to get the value by passing in the key value as clover which the map does not have now because it has already calculates the hash code according to the previous string which was arthur.

hope this helps
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


d1.name was changed on "line", new Dog("clover") doesn't "equals" to d1 anymore, even though their hashCode() are same.
assuming you know the context of Dog object definition:
 
Venkat Sidh
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James,

Are you saying #3 fails or succeeds ?
 
James Quinton
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Venkat Sidh:
James,

Are you saying #3 fails or succeeds ?



fails, because the keys are not same anymore since equals() method returns false (clover not equals to arthur) even though both hashCode() methods return same value (clover length equals to arthur length)
[ October 18, 2006: Message edited by: James Quinton ]
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Map just holds references to the objects, not the objects itself. So, when an object is changed, then those changes will be reflected in the map.
 
Venkat Sidh
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got what you guys are talking about.

I'm under impression that though d1.name changed to 'arthur' it will not affect m.get(new Dog("clover"))) as we're passing a new instance of Dog as a Key.

Have one more question though,



here what is the Key passed in line #2..is it reference to Dog (d1) or new object of Dog

Thanks.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic