aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HashMap Duplicate Element Problem 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 "HashMap Duplicate Element Problem" Watch "HashMap Duplicate Element Problem" New topic
Author

HashMap Duplicate Element Problem

Soumya Ranjan Mohanty
Ranch Hand

Joined: Mar 07, 2010
Posts: 44


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

Example from Cathy Siera Book.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Please UsecodeTags when posting code. I've added them for you this time.


[My Blog]
All roads lead to JavaRanch
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

the output becomes 2 why??

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

Joined: Nov 04, 2009
Posts: 2066

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.

.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Soumya Ranjan Mohanty
Ranch Hand

Joined: Mar 07, 2010
Posts: 44
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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.
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634


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





OCPJP 6.0 93%
OCPJWCD 5.0 98%
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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...
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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


SCJP6.0,My blog Ranchers from Delhi
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Mar 17, 2010
Posts: 1051

@Abimaran Kugathasan


Could you please telll how hashcode works thenn,,,,,.........
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks Henry.
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

The code doesnot compile :

error:

java:4: type Map does not take parameters
Map<ToDos,String>m=new TreeMap<ToDos,String>();
^
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: May 18, 2010
Posts: 634

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

Joined: May 18, 2010
Posts: 634

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

Joined: Nov 04, 2009
Posts: 2066

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
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

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
Katherine Reut
Greenhorn

Joined: Jul 26, 2013
Posts: 28
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap Duplicate Element Problem