• 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

Chapter 7: Generics and Collections K and B pg no 584

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


i have some doubts in this code.
As far as i understand "The Map interface maps unique keys to value means it associate value to unique keys which you use to retrieve value at a later date."
when we see the first output "System.out.println(m.get("k1"));" it printsthe hashcode and not the aiko
But this line prints the System.out.println(m.get(d1)); // #4 the dog key
As hashMap returns the value of the key then why its not returning in the first case..please explain how hashMap works..
thanks in advance
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that will call the toString method of the object mapped to the key k1. If toString is not overridden for an object, you will get that "hash" string. So, your Dog object needs to override toString()

maps the key d1 to a String containing "Dog key" , so no surprise that it prints the string.

Pets.DOG is just a string, so it prints the string
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic