编写compareTo的正确实现

问题描述 投票:1回答:2
private static class CharacterIndex implements Comparable<CharacterIndex> {
    private final char c;
    private final int index;

    public CharacterIndex(char c, int index) {
        this.c = c;
        this.index = index;
    }
}

现在我想覆盖这个类的compareTo方法,这样如果我有List的对象CharacterIndex[('y', 1), ('x', 2), ('b', 3), ('a', 3)],那么在排序之后,对象应该像[('b', 3), ('a', 3), ('x', 2), ('y', 1)]

排序策略:

索引不同的对象可以进行混洗(并且应该仅根据字符的值按排序顺序)。具有相同索引的对象应在排序后保持其相对顺序。

One more example:

对于[('y', 1), ('w', 2), ('x', 3)],排序列表应该是[(w, 2), (x, 3), (y, 1)]而不是[(x, 3), (w, 2), (y, 1)]

我的尝试:

@Override
public int compareTo(CharacterIndex ci) {
    if (this.index == ci.index)
        return -1; // don't swap if the index is same
    return Character.compare(this.c, ci.c);
}

但这种方法给了我一个例外:

Exception in thread "main" java.lang.IllegalArgumentException: Comparison method violates its general contract!
    at java.util.ComparableTimSort.mergeHi(ComparableTimSort.java:866)
    at java.util.ComparableTimSort.mergeAt(ComparableTimSort.java:483)
    at java.util.ComparableTimSort.mergeForceCollapse(ComparableTimSort.java:422)
    at java.util.ComparableTimSort.sort(ComparableTimSort.java:222)
    at java.util.Arrays.sort(Arrays.java:1312)
    at java.util.Arrays.sort(Arrays.java:1506)
    at java.util.ArrayList.sort(ArrayList.java:1462)
    at java.util.Collections.sort(Collections.java:141)

我看到了this。但我无法清楚地理解为什么我会得到这个例外。

是否有更好的方法用上面给出的策略对List<CharacterIndex>进行排序?

java sorting comparable custom-compare
2个回答
1
投票

将@ RealSkeptic的评论置于答案中:

The java-docs of Comparable says that:

此接口对实现它的每个类的对象强加一个总排序。

Total-ordering should follow the following properties:

  • 自反性
  • 反对称
  • 及物
  • 可比性

要查看有关完全有序集的更多信息,请参阅this链接。

My sorting strategy is not transitive.

假设我的初始列表是[(d,2), (y,1), (b,1)]

  • (y,1) < (b,1)作为相同的指数,我不想洗牌顺序。
  • (b,1) < (d,2)对于不同的索引,我可以随机播放顺序,然后我只需要比较字符。

通过传递性,(y,1) < (d, 2)。哪个不是真的。

所以这不是一个完全有序的比较。因此它打破了Comparable界面提供的规则。

因此,对于这种排序策略,我们无法正确实现compareTo(遵守Comparable接口的合同)。

What you learned?

您的排序策略应始终定义实现Comparable的类对象的总排序


0
投票

这是因为如果o1.compareTo(o2) == -1 && o2.compareTo(o1) == -1你有o1.index == o2.indexcompareTo方法的合同是这两个必须有不同的标志:o1.compareTo(o2)o2.compareTo(o1)

换句话说,这意味着o1小于o2o2小于o1,这是不可能的。

可能的方法:

@Override
public int compareTo(CharacterIndex ci) {
    return Character.compare(this.c, ci.c);
}

在这里,我们通过角色进行比较。正如@RealSkeptic注意到的那样,由于关系不再具有传递性,因此无法将索引考虑在内。

© www.soinside.com 2019 - 2024. All rights reserved.