Java 11 中的 CharSequence 接口添加了方法 `compare`。为什么不是 Comparable 接口的 compareTo 呢?
CharSequence interface in Java 11 added method `compare`. Why not `compareTo` of Comparable interface?
CharSequence
interface gained a new static method in Java 11: compare
。
这个方法returns一个int
:
the value 0 if the two CharSequence are equal; a negative integer if the first CharSequence is lexicographically less than the second; or a positive integer if the first CharSequence is lexicographically greater than the second.
这听起来就像 compareTo
of Comparable
。然而 Java 团队显然选择了 而不是 让 CharSequence
扩展 Comparable
。为什么不?逻辑逃脱了我。
➥ CharSequence::compare
有什么不适合 Comparable::compareTo
的?
自 String
implements CharSequence
and Comparable<String>
以来,添加 Comparable<CharSequence>
将无法正常工作。
有关讨论,请参阅一位 OpenJDK 开发人员关于该主题的 this post。
post 的提示:CharSequence::compare
would be suitable as a Comparator
形式的方法参考。
CharSequence
interface gained a new static method in Java 11: compare
。
这个方法returns一个int
:
the value 0 if the two CharSequence are equal; a negative integer if the first CharSequence is lexicographically less than the second; or a positive integer if the first CharSequence is lexicographically greater than the second.
这听起来就像 compareTo
of Comparable
。然而 Java 团队显然选择了 而不是 让 CharSequence
扩展 Comparable
。为什么不?逻辑逃脱了我。
➥ CharSequence::compare
有什么不适合 Comparable::compareTo
的?
自 String
implements CharSequence
and Comparable<String>
以来,添加 Comparable<CharSequence>
将无法正常工作。
有关讨论,请参阅一位 OpenJDK 开发人员关于该主题的 this post。
post 的提示:CharSequence::compare
would be suitable as a Comparator
形式的方法参考。