• 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

HashMap Duplicate Element Problem

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


When the hashCode() uncommented , the output becomes 2 why??

Example from Cathy Siera Book.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UsecodeTags when posting code. I've added them for you this time.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the output becomes 2 why??


Before doing this exercise, what did you learn about Maps ? Do you remember how objects are stored in maps ?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your case, if you don't override the hashCode() method, it'll be inherited from Object class. When you comment that hashCode() method, default hashCode() method will be invoked to determine whether the key object are same or not, according to that(default) hashCode() method, those three objects are different. And If you override the hashCode() method, according to your hashCode() method, those three object will come to the same bucket. Then to verify whether those three object are different or not, the equals method will be invoked. According to your equals() method, there are two different objects. So the answer is 2.

Check by printing the Map.

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

Christophe Verré wrote:

the output becomes 2 why??


Before doing this exercise, what did you learn about Maps ? Do you remember how objects are stored in maps ?



i was confused for the size of HashMap, is it only counts the number of discrete objects in a Map after the correct implementation of HashCode ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Soumya Ranjan Mohanty wrote:i was confused for the size of HashMap, is it only counts the number of discrete objects in a Map after the correct implementation of HashCode ?


You should have learned that Objects are stored in buckets, which are identified by the hashCode. What happens to two equal objects when they are put in the same bucket ? Abimaran explained this in his post.
 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Which is correct? (Choose all that apply.)
A. As the code stands it will not compile
B. As the code stands the output will be 2
C. As the code stands the output will be 3
D. If the hashCode() method is uncommented the output will be 2
E. If the hashCode() method is uncommented the output will be 3
F. If the hashCode() method is uncommented the code will not compile


ans is :C ANd D
i think answer should be C and E

hasCode() determines the bucket,so all object would be placed in the same bucket.
Map can hold duplicate entries
and as far i know equals is used so that when we use the
get function to retrieve the data ,the passed object is same as which we
used in the



 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohitkumar gupta wrote:
i think answer should be C and E

hasCode() determines the bucket,so all object would be placed in the same bucket.
Map can hold duplicate entries
and as far i know equals is used so that when we use the
get function to retrieve the data ,the passed object is same as which we
used in the


Don't you go through those explanation? If Map holds duplicate elements, how can you retrieve your data?

Map cares about unique identifiers. You map a unique key to a specific value. HashMap allows one null key and multiple null object. Hashtable don't let you have anything null. the hashCode() and equals() methods are invoked when you put an key/object pair on a Map to check whether to allow that particular object or not...
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Soumya Ranjan Mohanty wrote:

When the hashCode() uncommented , the output becomes 2 why??

Example from Cathy Siera Book.




when implemeting hashcode...........please see how hascode works.........

ToDos t1 = new ToDos("Monday");
ToDos t2 = new ToDos("Monday");
ToDos t3 = new ToDos("Tuesday");
first see this will call this method
ToDos(String d) { day = d; }
which will in turn call equals method...........
after equals.....then hascode method will be call...........as it is hascode is commented then it wil call the hashcode method of object class.
which will return some random value........
m.put(t1,"doLaundry");
m.put(t2, "payBills");
m.put(t3, "cleanAttic");
so all these three will be treated as different............the m.size() will return 3

but when we uncomment that.........then hashcode method will return same hascode for 2 Monday and 1 hashcode for Tuesday
So the m.size() will return key value mappings in the map......
which is 2........


but when we are
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

phil sohan wrote:
when implemeting hashcode...........please see how hascode works.........

ToDos t1 = new ToDos("Monday");
ToDos t2 = new ToDos("Monday");
ToDos t3 = new ToDos("Tuesday");
first see this will call this method
ToDos(String d) { day = d; }
which will in turn call equals method...........
after equals.....then hascode method will be call...........as it is hascode is commented then it wil call the hashcode method of object class.
which will return some random value........
m.put(t1,"doLaundry");
m.put(t2, "payBills");
m.put(t3, "cleanAttic");
so all these three will be treated as different............

but when we uncomment that.........then hashcode method will return same hascode for 2 Monday and 1 hashcode for Tuesday
So the m.size() will return key value mappings in the map......
which is 2........


but when we are



No, you are wrong!
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Abimaran Kugathasan


Could you please telll how hashcode works thenn,,,,,.........
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

phil sohan wrote:@Abimaran Kugathasan
Could you please telll how hashcode works thenn,,,,,.........



I think, my first reply in this question is correct. Please Have a look on it.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:

phil sohan wrote:@Abimaran Kugathasan
Could you please telll how hashcode works thenn,,,,,.........



I think, my first reply in this question is correct. Please Have a look on it.




While this is an interesting debate, keep in mind that the HashMap requires that the hashCode() / equals() contract be adhered to (and there is no checking for it). If the contract is not adhered to, then the map is broken. And this debate is simply trying to speculate how something that is broken is behaving.

It may be easier to answer the OP question with... the map is broken, so it's not behaving correctly.


But... Abimaran is more correct. The hashCode() method is used first, to determine the bucket to store the key. And only if there are other keys in the bucket, is the equals() method used to determine equality (to try to guarantee no duplicates).

Henry
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code doesnot compile :

error:

java:4: type Map does not take parameters
Map<ToDos,String>m=new TreeMap<ToDos,String>();
^
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Are you using a 1.5 compiler ?
2. Do you have a class called Map in your classpath ?
3. Next time, start a new thread. This has nothing to do with the original post.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.I am using jdk1.6
2.i don't have a class called Map in my classpath.
3.This is part of the question posted in this thread.
see line 4 of the code
4.The answer given in the book is C and D,but as it doesn't compile,the answer should be A
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone tell me,whether equals or hascode method would be called when the following line is encountered in the code





i am not able to get ,how the objects t1,t2 are considered same.

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohitkumar gupta wrote:can anyone tell me,whether equals or hascode method would be called when the following line is encountered in the code





i am not able to get ,how the objects t1,t2 are considered same.



Neither hashCode() nor equals() called when you creating these object, but they(hashCode() & equals()) will be invoked when you put these objects into that Map(in your case) to check whether they are considered same or not. According your hashCode() and equals() methods, the object t1 and t2 are same. So only one can be in that Map.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohitkumar gupta wrote:
3.This is part of the question posted in this thread.
see line 4 of the code
4.The answer given in the book is C and D,but as it doesn't compile,the answer should be A




While I agree that this issue was caused by this topic, you have to admit that you are having an error that no-one else is having. No one here is having a problem compiling this code, and hence, on a completely different track in the discussion. It may be better to start a new topic to try to resolve the issue of why you can't compile the code.

Henry
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote: According to your equals() method, there are two different objects. So the answer is 2. Check by printing the Map.



I am confused about what happens to the duplicate elements after they have been added to the map. I printed the map after each element is added, and then again at the end.



The output is:


It looks as though all three elements are added to the map, but after the second Monday (t2) is added, the first Monday (t1) either disappears or is lost. How does this work? Is the first Monday still in the Map, but it is hidden by the second Monday? Or does the first Monday get removed when the second Monday is added?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic