gpt4 book ai didi

java - 重写compareTo有什么影响?

转载 作者:行者123 更新时间:2023-12-02 00:27:15 26 4
gpt4 key购买 nike

我知道如果重写 equals,hashCode 也应该被重写。是否有任何类似的规则适用于覆盖compareTo?

这是一个Java问题。

最佳答案

它的期望可以在这里阅读:http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

您最感兴趣的部分可能是:

It is strongly recommended (though not required) that natural orderings be consistent with equals. This is so because sorted sets (and sorted maps) without explicit comparators behave "strangely" when they are used with elements (or keys) whose natural ordering is inconsistent with equals. In particular, such a sorted set (or sorted map) violates the general contract for set (or map), which is defined in terms of the equals method.

关于java - 重写compareTo有什么影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9757047/

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