| 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: 819
|
|
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
|
 |
 |
|
|
subject: Chapter 7: Generics and Collections K and B pg no 584
|
|
|