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 ...
一种使用惰性图归约来理解非严格功能语言的实现的实用方法。
操作系统—线程,对轻量级线程的实现进行讲解,涉及到线程调度、线程同步、信号模型等
Item 12: Prefer Member Initializers to Assignment Statements 74 Item 13: Use Proper Initialization for Static Class Members 77 Item 14: Minimize Duplicate Initialization Logic 79 Item 15: Utilize ...
Information Retrieval Implementing and Evaluating Search Engines Stefan B¨uttcher Charles L. A. Clarke Gordon V. Cormack
Credit Risk Scorecards:Developing and Implementing Intelligent Credit Scoring
考试 AI-102: Designing and Implementing a Microsoft Azure AI Solution 考试 AI-102 的候选人应拥有利用 Azure Cognitive Services、Azure Cognitive Search 和 Microsoft Bot 框架构建、管理和部署 AI 解决方案...
Apress Pro WCF 4 Practical Microsoft SOA Implementation, 2nd Part I: Introducing Windows ...■Chapter 12: Developing Peer-to-Peer Applications with WCF ■Chapter 13: Implementing SOA Interoperability
本文档为Exam 70–305: Developing and Implementing Web Applications with Microsoft Visual Basic .NET and Microsoft Visual Studio .NET的配套练习题集。每个题目都有答案及详细的解释。考MCSD的朋友不妨试试。
行业报告
这是一本讲诉搜索开发技术的书,在亚马逊上评分为五星。众所周知,搜索引擎很多技术都很成熟了,但其实践的成本很高,因此,很多相关书籍讲解的大多是概念,而计算机科学的内核是实践,没法动手做的东西,意义都不大...
Wiley - Implementing NAP and NAC Security Technologies(Apr 2008).pdf
Implementing cryptography requires integers of significant magnitude to resist cryptanalytic attacks. Modern programming languages only provide support for integers which are relatively small and ...
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删除 查看此书详细信息请在美国亚马逊官网搜索此书
高清彩版 Android User Interface Design Implementing Material Design for Developers(2nd)
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-...
This application note shows how to create µVision DLLs to interface to hardware debuggers. The description is useful for developers of hardware emulators and debugger interfaces. An example program ...