• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collection Example in K&B

 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you supposed to be creating Dog or Doggy instances?
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure. Will keep that in mind.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic