aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B Book p583/ Generics and Collections / HashMap Sample code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B Book p583/ Generics and Collections / HashMap Sample code" Watch "K&B Book p583/ Generics and Collections / HashMap Sample code" New topic
Author

K&B Book p583/ Generics and Collections / HashMap Sample code

Vicky Wang
Greenhorn

Joined: Feb 10, 2013
Posts: 3
Hello,

The following code:


In the book, it's stated because Cat class does not overwrite hashCode() and equals(), Cat instance cannot be found.
However, when I tried keeping the reference to the cat obj, I get the "Cat key" output for the line above.

Can someone explain this?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30919
    
158

That's because you are using the same cat object when doing the get.

If you try this way instead, it should return null:


The same physical object (c1) returns the same has code so you get luck y and the Cat gets returned. In my example, it is a different Cat object as key and the luck is gone.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Vicky Wang
Greenhorn

Joined: Feb 10, 2013
Posts: 3
then I would assume the following code should be able to find Dog:



but the output is null.

Why is that?
Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Vicky Wang wrote: then I would assume the following code should be able to find Dog:



but the output is null.

Why is that?


Hi Vicky, that should not be null if you correctly override equals and hashCode method..


OCPJP 6 (91%)
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 797
    
    1
Vicky Wang wrote: then I would assume the following code should be able to find Dog:



but the output is null.

Why is that?


d1 has its unique hashcode in this example. For instance, the hashcode is 12345678.
The new Dog ("Charlie") has its own unique hashcode as well.
For instance this new dog object has hashcode = 87654321.

When the get method is called, it looks for hashcode 87654321 in the map, m . But there is no 87654321 hashcode in m. That was why it returns null.
Vicky Wang
Greenhorn

Joined: Feb 10, 2013
Posts: 3
Ankit Gareta wrote:
Vicky Wang wrote: then I would assume the following code should be able to find Dog:



but the output is null.

Why is that?


Hi Vicky, that should not be null if you correctly override equals and hashCode method..


I rewrote the same code on my linux machine and it found "Charlie" just fine.
Need to go back to my Mac and recheck code.
In any case, thanks for the reply!!
 
jQuery in Action, 2nd edition
 
subject: K&B Book p583/ Generics and Collections / HashMap Sample code