This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Maps confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Maps confusion" Watch "Maps confusion" New topic
Author

Maps confusion

Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
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.
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

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


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109

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

Joined: Jun 08, 2007
Posts: 109
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

Joined: Aug 31, 2007
Posts: 787

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

Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
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


SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
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

Joined: Jun 08, 2007
Posts: 109
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

Joined: Apr 24, 2009
Posts: 317
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

Joined: Aug 31, 2007
Posts: 787

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Maps confusion
 
Similar Threads
Why HashSet is adding duplicates even if I get same hashcode for two obects & passes true fromequals
Why am I being able to add Duplicates to this set?
Question from John Meyer's Mock
hashCode() and equals() - How it is possible
HashSet Duplicate element ?