File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What's wrong with this code? Why I am able to add duplicates with this code. 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 "What Watch "What New topic
Author

What's wrong with this code? Why I am able to add duplicates with this code.

Archna Singh
Greenhorn

Joined: Feb 11, 2012
Posts: 8


The output is
HashSet-- 2 3 1 1
LinkedHashSet--1 3 1 2
TreeSet--1 2 3
ArrayList--1 3 1 2

Why I am able to add duplicates? CAn someone help me?


[Edit - added code tags - see UseCodeTags for details]
Archna Singh
Greenhorn

Joined: Feb 11, 2012
Posts: 8
I caught my silly mistake (typo in hashCode)..its working fine now.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Good... In your next posts please UseCodeTags
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Thanks for the challenging question. I really appreciate it. It took me a long time to figure out, but it is worth it.
The problem comes from int hashcode() method.
Try to use public int hashCode() with a capital C instead.
Your MString does not override hashCode() method from the Object class.

Therefore, your MString ("1") , MString("3") and MString("1") and MString("2") all have unique hash codes.

Case 1 : when the hashCode() method is not overriden.
When you put MString objects into hash set or linked hash set, here are the steps implemented by hashset class or linkedhashset class :
1. MString("1") has a unique hashcode generated during runtime. For example, its hashcode is 1001. This object is put into a table in hashset class or linked hash set class. This table is called entry table, which is an array. Let's say, it will put entrytable [1001] = new MString("1");
2. MString ("3") has a unique hashcode. For example, its hashcode is 3001. Then, entrytable[3001]= new MString("3");
3. MString("1") has a UNIQUE hashcode. For example, its hashcode is 4001. Then, entrytable[4001]= new MString("1") and etc.
Therefore, you end up having 4 entries in the sets.

Case 2: when the hashCode() method is overriden.
1. MString("1") has a hash code which is 1. The entryTable[1] = new MString("1");
2. MString("3") has a hash code which is 1. The entryTable[1] has a value assigned as step 1. So, what should hash set do? Check if MString("3") equals MString("1"). They are not equal. Then, etnryTable[1] will have a list data structure, the first element in the list is MString("3") and the second one is MString("1"); Now, entryTable[1] has two objects. Just like a bucket has two balls.
3. MString("1") has a hash code which is also 1. The entryTable[1] has two values now. So, what should hash set do? Check if MString("1") equals the two values. Yes. It equals to another MString("1") that has exist. It is just like you have two balls, a red ball and a blue ball in the bucket. Now, you try to put another red ball in. But there exists a red ball in the bucket, so this second red ball won't be added.

For more detail or verify what I say , read the open source code about HashSet or HashMap developed by Oracle.


Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Hi, Archna. Don't say this is silly. That is a really really good exam question. In the mock exam, there was a question like this:
public boolean equals(MString s) { ... }
This method does not override equals in Object class. So, all the MStrings are considered as not equal. Because the Object's equals method compare the references of the objects. If the references refer to the same object, they are equal.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3527
    
  15
Of course the easy way to avoid such errors is to put @Override in front of any method you think is overriding another method. That way the compiler will tell you when you've made a typo.


Joanne
 
 
subject: What's wrong with this code? Why I am able to add duplicates with this code.