23

I have a quick question about TreeSet collections and hashCode methods. I have a TreeSet and I'm adding objects to it, before I add an object, I check to see if it exists in the TreeSet using the contains method.

I have 2 distinct objects, each of which produce a distinct hashCode using my implementation of the hashCode method, example below:

public int hashCode()
{
    int hash = 7;
    hash = hash * 31 + anAttribute.hashCode();
    hash = hash * 31 + anotherAttribute.hashCode();
    hash = hash * 31 + yetAnotherAttribute.hashCode();
    return hash;
}

The hashCodes for a particular run are: 76126352 and 76126353 (the objects only differ by one digit in one attribute).

The contains method is returning true for these objects, even though the hashCodes are different. Any ideas why? This is really confusing and help would really be appreciated.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Gaz
  • 3,855
  • 6
  • 28
  • 33

4 Answers4

52

TreeSet does not use hashCode at all. It uses either compareTo or the Comparator you passed to the constructor. This is used by methods like contains to find objects in the set.

So the answer to your question is that your compareTo method or your Comparator are defined so that the two objects in question are considered equal.

From the javadocs:

a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • It also uses the equals method, so it's important that equals and the Comparator/compareTo are consistent. – Dan Dyer Sep 24 '09 at 10:13
  • 2
    "This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all element comparisons using its compareTo (or compare) method ..." (from http://java.sun.com/javase/6/docs/api/java/util/TreeSet.html) – Dirk Sep 24 '09 at 10:36
  • 1
    this indeed was the problem, I added an extra attribute, updated equals and hashCode but forgot about compareTo. Thanks!! – Gaz Sep 24 '09 at 12:31
  • what if I am inserting an Integer object in TreeSet and contains() returns false even though an Integer with same value already exists in the TreeSet? – Nitish Upreti Oct 06 '10 at 18:14
4

From Java Doc:

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.

Means: the objects you use for hashing are not equal.

user207421
  • 305,947
  • 44
  • 307
  • 483
tuergeist
  • 9,171
  • 3
  • 37
  • 58
0

You need to read Joshua Bloch's "Effective Java" chapter 3. It explains the equals contract and how to properly override equals, hashCode, and compareTo.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

You don't need to checked if it is contained, because the insert() basically does the same operation (i.e. searching the proper position) on its way to the insertion point. If the object can't be inserted (i.e., the object is already contained), insert returns false.

helpermethod
  • 59,493
  • 71
  • 188
  • 276