• 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

Maps confusion

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

I am reviewing Maps from k&B , on Pg 563. It mentions that Classes used as part of the key for Maps must override equals and hashCode
method. If you do not then program will compile and run you just wont find your stuff. I just wrote a program which contradicts this!!



The output prints............
Find result is : ID12
Size is : 2

I am able to find using a key of class Student1 even though this class does NOT override equals and hashcode??
any help much appreciated.
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Meghna

You just try using new Student1("John") and see what happen?




Output is

Find result is : null
Size is : 2


Now if you override hashcode and equals method then the prope value will return by program.

Try yourself overriding both methods
 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks at least I am getting some predictable result now, but can you please explain to me, why its only true when
we use new Student1("John") and NOT for the reference variable s1??
 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok i modified my program but I made another change, please see below:



Now it prints:

Find result is : null
Find result for s1 is : ID12
Size is : 3


So it still finds the reference key s1?? Why does it find the key when you use s1? Thanks for your reply.
 
Ninad Kulkarni
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because when you are creating new objects the hashcode of each object is unique for each object you created.

The process to add or to find the object is as follows

In first step hashcode will give you the bucket meaning that location to search then you will find exact object using equals method by comparing equallity of objects.

Here you are using referrence of the object you already created and used as a key to retrive value. But when you create new object its hashcode value is different than original one because you are not overriding hashcode and equals method so it will look in different location and search for object which is not there so it will return null as a value

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

Meghna Bhardwaj wrote:HI ,
I am reviewing Maps from k&B , on Pg 563. It mentions that Classes used as part of the key for Maps must override equals and hashCode
method. If you do not then program will compile and run you just wont find your stuff. I just wrote a program which contradicts this!!


Where did you find that the objects you add to the Maps MUST override the equals and hashCode method? They inherit them. Isn't that enough?
No compiler error, nor any runtime error occur.
The other questions are answered already by the poster above me.

cheers
Bob
 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Bob,

I think you misunderstood me.... I did not mean objects you add to Map must override equals() and hashCode().
I am saying that the book mentions the KEY of the Map, i.e. Map<key, value>, the key object used MUST override
the methods. In my example code, Student1 is the Key in the Map.

I disagree that just because you add an object to Map it MUST override those methods...this is not correct.
 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Ninad,

Thanks for taking the time to answer my questions....sorry but I am still confused about the Map code sample above.
You stated:

Here you are using reference of the object you already created and used as a key to retrive value. But when you create new object its hashcode value is different than original one because you are not overriding hashcode and equals method so it will look in different location and search for object which is not there so it will return null as a value



I am following what you are saying and I get the whole analogy of the hashCode as the bucket where to look and the
equals method being the "name on the piece of paper" like it is explained in the book. I understand the null result printed I have NO
problems with that. But still the reference variable s1...why does it find it? I am losing sleep over this so please put me out of my misery!
Maybe you can refer me to some site where it is explained in more detail etc... anything that helps. thanks.
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Meghna Bhardwaj wrote:HI Bob,

I think you misunderstood me.... I did not mean objects you add to Map must override equals() and hashCode().
I am saying that the book mentions the KEY of the Map, i.e. Map<key, value>, the key object used MUST override
the methods. In my example code, Student1 is the Key in the Map.
I disagree that just because you add an object to Map it MUST override those methods...this is not correct.


Hi Meghna,
Still, i don't find in the book that the "key" class MUST override the equals() and hashCode(). Either you do it or not. The programmer has to live with the
consequences.
Bob
 
Ninad Kulkarni
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But still the reference variable s1...why does it find it? I am losing sleep over this so please put me out of my misery!
Maybe you can refer me to some site where it is explained in more detail etc... anything that helps. thanks.



Hi, Meghna

Because you didn't override hashcode and equals methods for Student1 class thats why you can't get "ID008" value.

See the following :

You created following objects and I will assume different names to buckets for understanding purpose

Student1 s1 = new Student1("John"); --> will refer bucket A
Student1 s2 = new Student1("Niall"); --> will refer bucket B
Student1 s3 = new Student1("John"); --> will refer Bucket C

and another new object you created in put method call is new Student1("John") and used it as a key at line number 11 in your program --> will refer bucket D

You used s1 as a key it will refer to bucket A only and it is different key than created new object in put method call.
You did't not store reference for new object so you can't reach bucket D to retrieve "ID008". you have no chance to retrieve it.
so your keys are really different they are not same objects so they are added as different keys in map because you didn't override hashcode and equals method
suppose if Student1 class overrides hashcode and equals methods then s1 will refer bucket A and the old value would be replaced by new value "ID008" and you will get "ID008" in get method call using s1 reference.

I hope this clears
 
reply
    Bookmark Topic Watch Topic
  • New Topic