gpt4 book ai didi

java - Java 11 中的 CharSequence 接口(interface)添加了方法 `compare` 。 Comparable接口(interface)的 `compareTo`为什么不是?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:59:55 27 4
gpt4 key购买 nike

CharSequence接口(interface)在 Java 11 中获得了一个新的静态方法:compare .

此方法返回一个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.

这听起来就像 compareToComparable .然而,Java 团队显然选择使CharSequence 扩展Comparable。为什么不?逻辑逃脱了我。

CharSequence::compare 有什么不适合 Comparable::compareTo 的地方?

最佳答案

添加Comparable<CharSequence> String 之后就无法正常工作了工具 CharSequence Comparable<String> .

有关讨论,请参阅 this post由一位 OpenJDK 开发人员就此主题提出。

该帖子的提示: CharSequence::compare 形式的方法引用适合作为 Comparator .

关于java - Java 11 中的 CharSequence 接口(interface)添加了方法 `compare` 。 Comparable接口(interface)的 `compareTo`为什么不是?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56299602/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com