aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Chapter 7: Generics and Collections K and B pg no 584 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Chapter 7: Generics and Collections K and B pg no 584" Watch "Chapter 7: Generics and Collections K and B pg no 584" New topic
Author

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

kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69


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

Joined: Apr 30, 2003
Posts: 820

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Chapter 7: Generics and Collections K and B pg no 584