1. compareTo is the sole method in the Comparable interface. By implementing Comparable, a class indicates that its instances have a natural ordering.
2. By implementing Comparable, you allow your class to interoperate with all of the many generic algorithms and collection implementations that depend on this interface. (Arrays.sort, Arrays.binarySearch, TreeSet, etc.)
3. The general contract of the compareTo method:
a) Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. Throws ClassCastException if the specified object’s type prevents it from being compared to this object.
b) The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception if and only if y.compareTo(x) throws an exception.)
c) The implementor must also ensure that the relation is transitive: (x.compareTo(y) > 0 && y.compareTo(z) > 0) implies x.compareTo(z) > 0.
d) Finally, the implementor must ensure that x.compareTo(y) == 0 implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all z.
e) It is strongly recommended, but not strictly required, that (x.compareTo(y) == 0) == (x.equals(y)). Violating this rule means this class has a natural ordering that is inconsistent with equals.
4. compareTo should throw ClassCastException if two object references being compared refer to objects of different classes. As of release 1.6, no classes in the Java platform libraries that support interclass comparisons.
5. A class that violates the compareTo contract can break other classes that depend on comparison. Classes that depend on comparison include the sorted collections TreeSet and TreeMap, and the utility classes Collections and Arrays, which contain searching and sorting algorithms.
6. There is no way to extend an instantiable class with a new value component while preserving the compareTo contract, unless you are willing to forgo the benefits of object-oriented abstraction. If you want to add a value component to a class that implements Comparable, don’t extend it; write an unrelated class containing an instance of the first class. Then provide a “view” method that returns this instance.
7. Consider the BigDecimal class, whose compareTo method is inconsistent with equals. If you create a HashSet instance and add new BigDecimal("1.0") and new BigDecimal("1.00"), the set will contain two elements because the two BigDecimal instances added to the set are unequal when compared using the equals method. If, however, you perform the same procedure using a TreeSet instead of a HashSet, the set will contain only one element because the two BigDecimal instances are equal when compared using the compareTo method.
8. Compare object reference fields by invoking the compareTo method recursively. If a field does not implement Comparable, or you need to use a nonstandard ordering, you can use an explicit Comparator instead.
9. Compare integral primitive fields using the relational operators < and >. For floating-point fields, use Double.compare or Float.compare in place of the relational operators, which do not obey the general contract for compareTo when applied to floating point values. For array fields, apply these guidelines to each element.
10. If a class has multiple significant fields, the order in which you compare them is critical. You must start with the most significant field and work your way down. If a comparison results in anything other than zero (which represents equality), you’re done; just return the result. If the most significant fields are equal, go on to compare the next-most-significant fields, and so on. If all fields are equal, the objects are equal; return zero.
相关推荐
Item 14: Consider implementing Comparable 4 Classes and Interfaces Item 15: Minimize the accessibility of classes and members Item 16: In public classes, use accessor methods, not public fields Item ...
### 实现功能语言:教程 #### 一、引言 《实现功能语言:教程》是一本专注于非严格功能语言实现的书籍,它提供了一种基于惰性图归约的实用方法来理解和实施这类语言。本书适合对计算机科学尤其是函数式编程领域感...
### 操作系统中的轻量级线程实现 #### 引言 本文介绍了一种轻量级线程库的实现方式,该线程库能够在单一UNIX进程中提供极其轻量级的线程,并允许对系统资源进行全面并发访问。这些线程足够轻量级,可以快速创建,...
Information Retrieval Implementing and Evaluating Search Engines Stefan B¨uttcher Charles L. A. Clarke Gordon V. Cormack
读书笔记:Implementing DomainDriven Design 实现领域驱动设计 代码示例
### BigNum Math: Implementing Cryptographic Multiple Precision Arithmetic In today's digital world, cryptography plays a crucial role in securing information and ensuring privacy. One fundamental ...
读书笔记:Implementing.DomainDriven.Design实现领域驱动设计 中英文对照翻译
《Credit Risk Scorecards:Developing and Implementing Intelligent Credit Scoring》这本书由Naeem Siddiqi撰写,通过深入浅出的方式向读者介绍了如何开发和实施智能信用评分系统。 #### 二、信用风险评分卡的...
**Item 12: Prefer Member Initializers to Assignment Statements** - **Advantages:** Member initializers provide a concise way to initialize fields. - **Example:** ```csharp public class Person { ...
行业报告
Wiley - Implementing NAP and NAC Security Technologies(Apr 2008).pdf
- **考试名称**:Developing and Implementing Web Applications with Microsoft Visual Basic .NET - **考试目标**:本考试主要针对希望使用Microsoft Visual Basic .NET 和 Microsoft Visual Studio .NET 开发与...
Android User Interface Design Implementing Material Design for Developers(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
Android User Interface Design Implementing Material Design for Developers(2nd) 英文mobi 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
VMware vRealize Automation Handbook Implementing Cloud Management in the Enterprise Environment 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索...
Title: Implementing Cloud Design Patterns for AWS Author: Marcus Young Length: 234 pages Edition: 1 Language: English Publisher: Packt Publishing Publication Date: 2015-05-15 ISBN-10: 1782177345 ISBN-...
在设计和实现微软Azure人工智能解决方案(AI-102)的考试中,考生需要具备使用Azure认知服务、Azure认知搜索以及Microsoft Bot框架构建、管理和部署AI解决方案的专业技能。这一领域涵盖了广泛的IT知识点,包括但不...