Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HashSet allows duplicates

I can't seem to get a HashSet instance to work as expected. The code I used is as follows:

import testing.Subclass;
import java.util.HashSet;

public class tester {
  public static void main(String[] args) throws Exception {
    HashSet<Subclass> set = new HashSet<Subclass>();
    set.add(new Subclass("007812"));
    set.add(new Subclass("007813"));
    System.out.println("Set size " + set.size());
    set.add(new Subclass("007812"));
    System.out.println("Set size " + set.size());

    for(Subclass sub : set) {
      System.out.println(" sub acctNbr " + sub.getAcctNbr());
    }
  }
}

Subclass

public class Subclass implements Comparable<Subclass> {

  public Subclass(String acctNbr) {
    this.acctNbr = acctNbr;
  }
  private String acctNbr;
  public String getAcctNbr() {
    return this.acctNbr;
  }
  public int compareTo(Subclass other) {
    return this.getAcctNbr().compareTo(other.getAcctNbr());
  }

  public boolean equals(Subclass other) {
    if(other.getAcctNbr().equals(this.getAcctNbr()))
      return true;
    else
      return false;
  }
  public int hashCode() {
    return acctNbr.hashCode();
  }
}

This code outputs

sross@sross-workstation:~/Documents$ javac testing/Subclass.java
sross@sross-workstation:~/Documents$ javac tester.java
sross@sross-workstation:~/Documents$ java tester
Set size 2
Set size 3
 sub acctNbr 007812
 sub acctNbr 007812
 sub acctNbr 007813
sross@sross-workstation:~/Documents$
like image 969
Sheldon Ross Avatar asked Nov 02 '09 20:11

Sheldon Ross


People also ask

Which set allow duplicates in Java?

Duplicates : ArrayList allows duplicate values while HashSet doesn't allow duplicates values. Ordering : ArrayList maintains the order of the object in which they are inserted while HashSet is an unordered collection and doesn't maintain any order.

Do sets allow for duplicates?

A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction.

Why does set donot allow duplicates?

The meaning of "sets do not allow duplicate values" is that when you add a duplicate to a set, the duplicate is ignored, and the set remains unchanged. This does not lead to compile or runtime errors: duplicates are silently ignored. Set is implemented like that to avoid duplication.

How does HashSet find duplicates in Java?

If the hashcode of two objects are equal then hashset uses equal() to see if the hashcode matched objects are really equal. And if they are equal the hashset knows that the new object is duplicate of something exist in the HashSet. And the add does not happen. The add() of hashcode returns false.


1 Answers

You need to override equals(Object). Instead of doing this you've implemented an equals method with signature equals(Subclass). Consequently your HashSet is using the default equals(Object) method defined on Object for equality testing.

The default equals(Object) implementation is based on object identity, and hence the set "allows" you to add two Strings that, whilst semantically equal, are not the same object.

like image 193
Adamski Avatar answered Oct 15 '22 22:10

Adamski