- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道如果重写 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/
这似乎很奇怪,这并没有像我预期的那样工作。我编写了一个简单的java类,它实现Comparable接口(interface)并重写compareTo()方法。但是,它不允许我传递除对象之外的特定类型的
public int compareTo(Object another) throws CustomMadeException { if(this.getClass() != another.
早些时候,我在通用 IComparable 中获得了逆变的具体示例Jon Skeet 的界面。然而,这又产生了另一个问题。为什么不是通用的 List.Sort()方法能够推断出相同的信息? 我在这里提
我有注释 package javaannotationtest; import java.lang.annotation.*; @Target({ElementType.METHOD}) @Reten
我试图理解 C# 中的 CompareTo(),下面的示例让我比以往任何时候都更加困惑。有人能帮我理解为什么第三个变体的结果是 1 吗?句子“Hello wordd”中的第二个词与 str1“Hell
我已经在这个程序上工作了几天,并且在我的 BinarySearchTree 类中实现了一些主要方法,例如插入和删除。插入似乎工作正常,但是一旦我尝试删除,我就会不断出错。因此,在玩弄了代码之后,我想测
首先,我必须提到的是,在阅读了大量问题和教程并观看了一些视频后,问题仍然没有解决。 我是一名Java中级程序员,我编写了一些用于比较优先级队列中的元素的代码,其中元素的保存方式类似于[Comparab
我只是在为即将到来的考试做一些复习。我发现了我们的讲师给我们提供的这段代码供我们修改。 代码: public class Employee implements Comparable{ private
我的书要求我为一段代码编写 Javadoc 注释。大多数情况下,我了解如何执行 javadocs,但我不了解该程序在做什么。 “为类 Person 的以下方法编写 Javadoc 注释。假设类 Per
如果我写以下内容是否有可能溢出: public class SomeObj implements Comparable { private final float data; pub
我有一个名为任务的类,我想将其放入 PriorityQueue 中。 我的类(class)通过日期和名为isUrgent的 boolean 字段进行比较 @Override publ
这个问题已经有答案了: Java error: Comparison method violates its general contract (13 个回答) 已关闭 7 年前。 我有这个compa
我的类(class)结构: public class Priorityy implement Comparable { public int compareTo(Object pe) {
给定一个非负整数列表,我想对它们进行排列,使它们形成最大的数字。给定 [1, 20, 23, 4, 8],最大的形成数字是 8423201。但我想弄清楚compareTo 方法中变量的顺序如何影响 A
我想通过上次联系日期比较两个“收件人”,如果相同,则通过地址进行比较。这是我的代码: public class RecipientComparator implements Comparator {
package Comparable; public class Movie implements Comparable { private double rating; privat
您好,我在实现compareTo 方法时遇到问题。我一直在寻找答案,但没有任何帮助。我正在尝试用各种大小的圆圈填充 TreeSet。我需要在我的圈子类中使用compareTo 才能以这种方式存储它们。
我有以下代码;目的是返回数组中按字母顺序排列的最小成员。 public String smallest() { String smallest = ""; int i = 0; while(log[i
我有一个关于compareTo函数如何协助比较器进行排序的问题即 o1.compareTo(o2) 与 o2.compareTo(o1) 如果两个字符串相等,则此方法返回 0,否则返回正值或负值。如果
帮助我无法弄清楚compareTo函数。这就是我必须做的:编写一个compareTo函数,可用于根据以下内容按顺序放置产品到他们的零件号。也就是说,后面按字母顺序排列的零件号顺序大于按字母顺序排列较早
我是一名优秀的程序员,十分优秀!