• 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

Generics (Using Maps) KB exam book page 583- 586

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here is the code from that page:




Thank you..
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter when you add a key-value to a Map, the key-value is placed in a bucket based on the hash code. If after that you modify the key in such a way that it changes the hash code of the key, then your map gets sort of corrupt. The key-value is still in the Map but you will not be able to find it as its still in the old bucket in which it went when you added it...
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter,

d1.name = "magnolia"; //Are we modifying the actual key in the map??? the key referenced by d1???
System.out.println(m.get(d1));

In this case you are modifying object d1 and when you invoke m.get (d1), Hashmap invoke hash method on d1 object and no bucket are found in HashMap. Then return null.

d1 not referts to the actual key in the map. When you pass d1 to get method, hashmap invoke your hascode() method at d1 and if bucket is found at hash table, then invoke your d1 equals method to find a match object (based on equals mehtod not the reference).
 
Peter Swiss
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply Ankit and Sergio... I will let you know if I have more questions.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, i'm completely stuck trying to understand this:

if i change code line 65 above from:



To:



I get:

d1= Dog key

The equals method never got executed because i put but never got executed? how come? and why i'm not getting null instead of Dog key?

Thanks a lot.
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic