java 中字符串比较的传递性 属性
Transitivity property of String comparison in java
看看这段代码
class StringComparator implements Comparator<String> {
@Override
public int compare(String a, String b) {
if (a.length() == b.length()) {
return b.compareTo(a);
} else {
String ab = a + b;
String ba = b + a;
return ba.compareTo(ab);
}
}
}
ba.compareTo(ab) 有效,但 ab.compareTo(ba) 失败。它抛出一个 IllegalArgumentException 引用违反比较器合同。我相信这是由于不满足传递性 属性 的事实。有人可以解释 Java 如何在 Strings 的情况下使用传递性 属性 吗?这与 Timsort 的工作原理有什么关系吗?
编辑:这是我在 Leetcode 在线评判中遇到的错误
Runtime Error Message:
Line 32: java.lang.IllegalArgumentException: Comparison method violates its general contract!
Last executed input:
[7286,155,351,6059,9686,2668,9551,5410,7182,170,3746,3095,8139,2587,2351,2341,2038,3956,6034,4071,9473,281,9306,8746,7954,8937,7855,3938,9737,2455,4344,2986,8968,1072,2442,7191,9106,4236,2768,5214,7541,329,7530,9068,9644,3539,5177,5332,2065,8245,7494,8454,604,4632,1745,301,3412,1569,8637,7840,7752,9536,1023,4841,1286,6489,8459,2725,8021,5026,7058,4540,9892,5344,1205,4363,959,9729,9225,9733,8417,9873,3721,1434,5136,6111,6189,780,4741,2670,2457,5424,1040,3746,1229,8568,3636,1546,2553,575]
同样,我在使用 ba.compareTo(ab) 时没有收到此错误。我
是的,如果 a
和 b
的长度不同,您的 compare
似乎不尊重传递 属性。
这是Comparator.compare(a,b) contract,请参阅有关传递性的粗体部分:
Compares its two arguments for order. Returns a negative integer,
zero, or a positive integer as the first argument is less than, equal
to, or greater than the second. In the foregoing description, the
notation sgn(expression) designates the mathematical signum function,
which is defined to return one of -1, 0, or 1 according to whether the
value of expression is negative, zero or positive.
The implementor must ensure that sgn(compare(x, y)) == -sgn(compare(y,
x)) for all x and y. (This implies that compare(x, y) must throw an
exception if and only if compare(y, x) throws an exception.)
The implementor must also ensure that the relation is transitive:
((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.
Finally, the implementor must ensure that compare(x, y)==0 implies
that sgn(compare(x, z))==sgn(compare(y, z)) for all z.
It is generally the case, but not strictly required that (compare(x,
y)==0) == (x.equals(y)). Generally speaking, any comparator that
violates this condition should clearly indicate this fact. The
recommended language is "Note: this comparator imposes orderings that
are inconsistent with equals."
更新:
只是为了添加一个有趣的旁注,您将 Tim 排序称为 Collections.sort() 等应用的排序算法,适用于较小的数组(大小低于硬编码阈值) ,而是执行简单的归并排序。选择是在排序方法的开头进行的,请参阅 openJDK 源以获取更多信息。
假设我们有三个字符串格式的数字a、b、c,a | b | c
是可以获得的最大结果。
然后我们知道(a | b) > (b | a)
和(b | c) > (c | b)
。
假设(c | a) > (a | c)
(无传递性),则我们有:
(c | a | b) > (a | c | b) > (a | b | c)
。这个矛盾a | b | c
是最大的。
看看这段代码
class StringComparator implements Comparator<String> {
@Override
public int compare(String a, String b) {
if (a.length() == b.length()) {
return b.compareTo(a);
} else {
String ab = a + b;
String ba = b + a;
return ba.compareTo(ab);
}
}
}
ba.compareTo(ab) 有效,但 ab.compareTo(ba) 失败。它抛出一个 IllegalArgumentException 引用违反比较器合同。我相信这是由于不满足传递性 属性 的事实。有人可以解释 Java 如何在 Strings 的情况下使用传递性 属性 吗?这与 Timsort 的工作原理有什么关系吗?
编辑:这是我在 Leetcode 在线评判中遇到的错误
Runtime Error Message:
Line 32: java.lang.IllegalArgumentException: Comparison method violates its general contract!
Last executed input:
[7286,155,351,6059,9686,2668,9551,5410,7182,170,3746,3095,8139,2587,2351,2341,2038,3956,6034,4071,9473,281,9306,8746,7954,8937,7855,3938,9737,2455,4344,2986,8968,1072,2442,7191,9106,4236,2768,5214,7541,329,7530,9068,9644,3539,5177,5332,2065,8245,7494,8454,604,4632,1745,301,3412,1569,8637,7840,7752,9536,1023,4841,1286,6489,8459,2725,8021,5026,7058,4540,9892,5344,1205,4363,959,9729,9225,9733,8417,9873,3721,1434,5136,6111,6189,780,4741,2670,2457,5424,1040,3746,1229,8568,3636,1546,2553,575]
同样,我在使用 ba.compareTo(ab) 时没有收到此错误。我
是的,如果 a
和 b
的长度不同,您的 compare
似乎不尊重传递 属性。
这是Comparator.compare(a,b) contract,请参阅有关传递性的粗体部分:
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. In the foregoing description, the notation sgn(expression) designates the mathematical signum function, which is defined to return one of -1, 0, or 1 according to whether the value of expression is negative, zero or positive.
The implementor must ensure that sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)
The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.
Finally, the implementor must ensure that compare(x, y)==0 implies that sgn(compare(x, z))==sgn(compare(y, z)) for all z.
It is generally the case, but not strictly required that (compare(x, y)==0) == (x.equals(y)). Generally speaking, any comparator that violates this condition should clearly indicate this fact. The recommended language is "Note: this comparator imposes orderings that are inconsistent with equals."
更新:
只是为了添加一个有趣的旁注,您将 Tim 排序称为 Collections.sort() 等应用的排序算法,适用于较小的数组(大小低于硬编码阈值) ,而是执行简单的归并排序。选择是在排序方法的开头进行的,请参阅 openJDK 源以获取更多信息。
假设我们有三个字符串格式的数字a、b、c,a | b | c
是可以获得的最大结果。
然后我们知道(a | b) > (b | a)
和(b | c) > (c | b)
。
假设(c | a) > (a | c)
(无传递性),则我们有:
(c | a | b) > (a | c | b) > (a | b | c)
。这个矛盾a | b | c
是最大的。