Search code examples
javagenericstreeset

Java: Problems with TreeSet


I have a class Odp. I want to use TreeSet to keep a sorted collection of Odp objects. However, I've been having problems.

public class OdpStorage {

    private TreeSet<Odp> collection = new TreeSet<Odp>(); 

    public addOdp(Odp o) {
          return collection.add(o);
    }

    public int size() {
          return collection.size();
    }

}

collection.add(Odp o) is supposed to do nothing if it's already in the tree, right? Somehow, this unit test fails:

OdpStorage ts = new OdpStorage();       
Odp ftw = new Odp("LOL");
    Odp ktr = new Odp("OMG");

    ts.addOdp(ftw);

    ts.addOdp(ftw); //should do nothing
    ts.addOdp(ftw); //should do nothing
    ts.addOdp(ftw); //should do nothing
    ts.addOdp(ktr);

assertEquals(2, ts.size());

The assertion fails. It expects 2, but the return value is 5. Why? Could the odp.equals() function be messed up?

Similarly, calling collection.contains(o) fails, even when the there is an object in the set X for which o.equals(X) returns true.

The .equals() function of Odp: (generated by Eclipse)

public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (!(obj instanceof Odp))
        return false;
    Gene other = (Odp) obj;
    if (sequence == null) {
        if (other.sequence != null)
            return false;
    } else if (!sequence.equals(other.sequence))
        return false;
    return true;
}

compareTo:

/**
 * this = g0
 * if they are equal, g1 is presumed to come first
 * 
 *  @return -1 if g0 comes before g1; 1 if g0 comes after g1
 */
@Override
public int compareTo(Odp g1) {

    if (sequence.length() < g1.getSeq().length()) {
        return -1;
    }
    else if (sequence.length() > g1.getSeq().length()) {
        return 1;
    }

    if (sequence.compareTo(g1.getSeq()) < 0) {
        return -1;
    }

    return 1;
}

hashCode() is not overridden. Problem?

UPDATE hashCode() is as follows:

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result
            + ((sequence == null) ? 0 : sequence.hashCode());
    return result;
}

But that still doesn't solve the problem.


Solution

  • It appears that your collection.add(o) is failing to find the object in the backing TreeMap. Does your Odp implement Comparable or are you setting a default Comparable on your TreeSet whose compare method you have implemented? If so, you will need to ensure that your compareTo (for the Comparable), or your Comparator compare method will return 0 if the objects passed in are equals.

    EDIT (in response to your comment to the original post):

    It is recommended that you override HashCode() whenever you override equals()

    EDIT2 in response to your compareTo implementation:

    If g0 and g1 are equal, you should return 0. This is the root of the problem.