• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

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


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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I caught my silly mistake (typo in hashCode)..its working fine now.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good... In your next posts please UseCodeTags
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
He was expelled for perverse baking experiments. This tiny ad is a model student:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic