aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collection Example in K&B 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 "Collection Example in K&B" Watch "Collection Example in K&B" New topic
Author

Collection Example in K&B

Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324


As per K&B pg: 583,584

The important point about first output is that the key used to retrieve the String was made of a Dog object. The second output is null.

The important point here is that the get() method failed to find the Cat object that was inserted earlier. Why dint we find the Cat key String? Why did it work to use an instance of Dog as key, when using an instance of Cat as a key failed?

It's easy to see that Dog overrode equals() and hashCode() while Cat didnt't.



But if I do the same treatment for Cat without overriding hashcode and equals, I get the result i.e



I can get the value "Cat key" in the output instead of null. So what does the explanation in K&B means.

Also, if I put the following code in the above program, it gives me null pointer while putting element in the map.



Can someone clarify me on this above behavior.


SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Himalay Majumdar wrote:
But if I do the same treatment for Cat without overriding hashcode and equals, I get the result i.e

// 2

I can get the value "Cat key" in the output instead of null. So what does the explanation in K&B means.

Also, if I put the following code in the above program, it gives me null pointer while putting element in the map.



Can someone clarify me on this above behavior.

When a class doesn't override equals() or hashCode() it gets the implementations of these methods in Object (assuming no other superclass overrode them.)
Why can you retrieve the cat? Because you are using a reference to the same object in lines // 1 and // 2.
Because of the contracts of hashCode() and equals() (the results must be consistent when called different times on the same object,) that means that you will get the same hashCode() and equals() results in // 1 and // 2

You say that you are getting a NullPointerException at line // 3. Would you mind posting the full code where this happens, and the error output that you get?


All code in my posts, unless a source is explicitly mentioned, is my own.
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
Ruben Soto wrote:
Why can you retrieve the cat? Because you are using a reference to the same object in lines // 1 and // 2.
Because of the contracts of hashCode() and equals() (the results must be consistent when called different times on the same object,) that means that you will get the same hashCode() and equals() results in // 1 and // 2

You say that you are getting a NullPointerException at line // 3. Would you mind posting the full code where this happens, and the error output that you get?


If you see my previous post, the K&B explanations says that get() method failed to find the Cat object that was inserted earlier because it dint override hashcode() or equals(). But, I think its because Cat and Dog object were NOT treated the same way in the main method and its NOT because Cat did not override hashCode() and equals()

while getting Dog the following was done



where as to get Cat object the following




if I treat the CAT same way as Dog, I can get it..i.e



but also ..If I treat Dog as Cat was treated initially I get null pointer error. i.e if I write




The following is the complete code that throws null pointer only when I override hashCode() and equals() in the Class Doggy



Again..there are two parts to my question.
1. I am thinking (as opposed to K&B) that get() method did NOT fail to find the Cat object that was inserted earlier because it dint override hashcode() or equals() but rather it was treated differently in main method(its in the code of my previous post)
2. Why I am getting Null pointer Exception above.

Dint want to write a bulky reply, but want to get my basic right. Thanks for taking out time for this Ruben.
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
just to update..I got the answer for 2nd question. Its because I was returning the hashcode that was depending on name.length, that was never initialized by the constructor. Am still waiting for clarifications over my first question. Thanks
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
That's exactly what's going on, Himalay. You needed to set name to something (even the empty string) in the default constructor, because otherwise name.length() will give you a NPE.

I'm going to modify your original code so that we can see things clearer:


In this case, the code for Dog and Cat operations are equivalent. You will still be able to retrieve the dog, but not the cat. In order to retrieve the cat you would need the exact same object for get() that you put in put(). With Dog it works because hashCode() returns the same hashcode even if the object is not exactly the same. And equals() will also return true if the objects a meaningfully equivalent.

Does that make sense now? I think the original code was obscuring the point a little bit.
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
I understood what you meant in your previous posted code. But the following code makes me think again.
I did not override equals and hashcode. I understand that if equals is not overriden, only the reference of the objects are checked for equality.

In the code below, I have just one reference. Still when I change the name(Actually name length) of the dog object d and then try to print it..it gives me null.




OUTPUT

Dog key
null <-- Isnt it suppose to return a value, as we are just checking by reference variable d1, that is unchanged.
Dog key
Dog key
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Are you supposed to be creating Dog or Doggy instances?
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
This is real silly of me. It was creating a object from the different clas in my project.
Thanks for pointing it out and clearing all my question Ruben.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
No problem, Himalay. My recommendation is to keep your examples as short and simple as possible, with as few concepts involved as possible. That way there is less possibility of error and you can get the points easier.
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
Sure. Will keep that in mind.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collection Example in K&B
 
Similar Threads
Doubt in collection
Generics (Using Maps) KB exam book page 583- 586
Issue in Working with Maps
Doubt in HashMap
equal() & hashcode()