Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HashSet contains duplicate entries

A HashSet only stores values ones, when the equals method says that they're the same. Thats what I thought.

But now i'm adding Elements to a HashSet where the equals method returns true and the size of the set still grows?? sorry I'm confused. Some hints where i'm wrong would be nice.

Element t1 = new Element(false, false, false, false);
Element t2 = new Element(true, true, true, true);
Element t3 = new Element(false, false, false, false);

if (t1.equals(t3))
    System.out.println("they're equal");

Set<Element> set = new HashSet<>();

set.add(t1);
set.add(t2);
set.add(t3);

System.out.println("set size: " + set.size());

so in this example my console output is:

they're equal
set size: 3

That makes no sense to me.. shouldn the size be 2?

like image 868
tObi Avatar asked Apr 26 '13 13:04

tObi


People also ask

Can a HashSet contain duplicates Java?

HashSet doesn't allow duplicates. If you try to add a duplicate element in HashSet, the old value would be overwritten. HashSet allows null values however if you insert more than one nulls it would still return only one null value.

Does HashSet contain unique values?

HashSet not only stores unique Objects but also a unique Collection of Objects like ArrayList<E>, LinkedList<E>, Vector<E>,..etc.

Why duplicates are not allowed in HashSet?

HASHTABLE is a structure of Key value pairs.. Here what the values passed by the SET is treated as Keys of HASHTABLE Internally. keys are unique cannot be duplicated. That is the reason if you pass any duplicate value it return false and does not added to the SET ...


2 Answers

The problem is that your Element class has not overridden the equals and hashCode methods or these implementations are broken.

From Object#equals method javadoc:

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true. It is consistent: for any non-null reference values x and y, multiple invocations of -x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

From Object#hashCode method javadoc:

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

Make sure the implementations of these methods satisfy these rules and your Set (backed by a HashSet) will work as expected.

like image 125
Luiggi Mendoza Avatar answered Oct 16 '22 13:10

Luiggi Mendoza


Your objects have different hashes so HashSet "puts" then in different "buckets".

like image 41
Lazarus Lazaridis Avatar answered Oct 16 '22 13:10

Lazarus Lazaridis