Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
kiruthigha rajan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • 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
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic