比较树集是否相等

compare treeset for equality

我有两组 HashSet,我将它们转换为 TreeSet 以对其进行排序以便于比较。将 HashSet 转换为 TreeSet 后。当我使用 'equals' 函数比较这两个 TreeSet 时,它说它们不同。我调试它,但它以相同的顺序显示相同的内容。我不明白哪里出了问题?

   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.
    }}

A 类给出如下:

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;
    }
}

编辑: 我也试过 if (treeSetA.containsAll(treeSetB) && treeSetB.containsAll(treeSetA) 这种情况。但结果相同,"Unequal set of tree"

来自 Java 比较器文档:

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.

你的比较器从不迎合平等的对象。此外,ClassA 不会覆盖 equals.

你的compare方法总是returns不平等。

来自文档:

[...]

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;
}

在输出中包含此结果

Equal set of tree

这里真正的教训是:研究您正在实现的接口。不要只是放下一些让编译器满意的代码。

您必须理解 @Override 比较函数的含义。因此,您为此研究 javadoc。这清楚地告诉您比较应该 return <0 , 0, >0。

这三个值之一。而且不只是 两个