user3042916
user3042916

Reputation: 87

compare treeset for equality

I have two sets of HashSet that i converted to TreeSet to sort it for ease of comparison. After converting HashSet to TreeSet. When i compare these two TreeSet using 'equals' function, it says they are different. I debug it, but it shows same content with same order. i can not understand what is wrong ?

   public class TestProductBundle {
    @SuppressWarnings("unused")
    public static void main(String args[]) {

        // HashSet
        Set<ClassA> hashSetA = new HashSet<ClassA>() {
            {
                add(new ClassA("name", 1, "desc"));
                add(new ClassA("name", 2, "desc"));
                add(new ClassA("name", 3, "desc"));
            }
        };

        Set<ClassA> hashSetB = new HashSet<ClassA>() {
            {
                add(new ClassA("name", 1, "desc"));
                add(new ClassA("name", 2, "desc"));
                add(new ClassA("name", 3, "desc"));
            }
        };

        TreeSet<ClassA> treeSetA = new TreeSet<ClassA>(new CompareID()) {
            {
                addAll(hashSetA);
            }
        };

        TreeSet<ClassA> treeSetB = new TreeSet<ClassA>(new CompareID()) {
            {
                addAll(hashSetB);
            }
        };

        if (treeSetA.equals(treeSetB))
            System.out.println("Equal set of tree");
        else
            System.out.println("Unequal set of tree");   // this is result.
    }}

ClassA gives below:

class ClassA {
String name;
int id;
String desc;

public ClassA(String name, int id, String desc) {
    this.name = name;
    this.id = id;
    this.desc = desc;
}

    int getId() {
        return id;
    }
}

class CompareID implements Comparator<ClassA> {
    @Override
    public int compare(ClassA o1, ClassA o2) {
        if (o1.getId() > o2.getId())
            return 1;
        else
            return -1;
    }
}

Edit: I tried if (treeSetA.containsAll(treeSetB) && treeSetB.containsAll(treeSetA) this condition also. But same result, "Unequal set of tree"

Upvotes: 1

Views: 3877

Answers (3)

GhostCat
GhostCat

Reputation: 140427

The real lesson here is: study the interfaces you are implementing. Don't just put down some code that makes the compiler happy.

You have to understand what it means when you @Override the compare function. So, you study the javadoc for that. And that clearly tells you that compare should return <0 , 0, >0.

One of those three values. And not just two of them.

Upvotes: 1

SomeJavaGuy
SomeJavaGuy

Reputation: 7347

Your compare method allways returns unequality.

from the doc:

[...]

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

[..]

public int compare(ClassA o1, ClassA o2) {
    if (o1.getId() > o2.getId())
        return 1;
    else if(o2.getId() > o1.getId())
        return -1;
    // 0  indicates equality.
    else return 0;
}

Including this results in the output

Equal set of tree

Upvotes: 3

Jose Ignacio Acin Pozo
Jose Ignacio Acin Pozo

Reputation: 882

From Java Comparator documentation:

The ordering imposed by a comparator c on a set of elements S is said to be consistent with equals if and only if c.compare(e1, e2)==0 has the same boolean value as e1.equals(e2) for every e1 and e2 in S.

Caution should be exercised when using a comparator capable of imposing an ordering inconsistent with equals to order a sorted set (or sorted map). Suppose a sorted set (or sorted map) with an explicit comparator c is used with elements (or keys) drawn from a set S. If the ordering imposed by c on S is inconsistent with equals, the sorted set (or sorted map) will behave "strangely." In particular the sorted set (or sorted map) will violate the general contract for set (or map), which is defined in terms of equals.

Your comparator never caters for equal objects. Also, ClassA does not override equals.

Upvotes: 0

Related Questions