Effective Java 12 Consider implementing Comparable
Sort array with sorted collection construction.
public class WordList {
public static void main(String[] args) {
Set<String> s = new TreeSet<String>();
// This will sort and filter the duplicated items in the string array automatically.
Collections.addAll(s, args);
System.out.println(s);
}
}
The interface
public interface Comparable<T> {
int compareTo(T t);
}
public final class CaseInsensitiveString
implements Comparable<CaseInsensitiveString> {
public int compareTo(CaseInsensitiveString cis) {
return String.CASE_INSENSITIVE_ORDER.compare(s, cis.s);
}
... // Remainder omitted
}
Implementation Key point
• 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.)
• The implementor must also ensure that the relation is transitive: (x.compareTo(y) > 0 && y.compareTo(z) > 0)implies x.compareTo(z) > 0.
• Finally, the implementor must ensure that x.compareTo(y) == 0 implies that
sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all z.
• It is strongly recommended, but not strictly required, that (x.compareTo(y)== 0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: This class has a natural ordering
that is inconsistent with equals."
Note
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.
// Normal implementation
public int compareTo(PhoneNumber pn) {
// Compare area codes
if (areaCode < pn.areaCode)
return -1;
if (areaCode > pn.areaCode)
return 1;
// Area codes are equal, compare prefixes
if (prefix < pn.prefix)
return -1;
if (prefix > pn.prefix)
return 1;
// Area codes and prefixes are equal, compare line numbers
if (lineNumber < pn.lineNumber)
return -1;
if (lineNumber > pn.lineNumber)
return 1;
return 0; // All fields are equal
}
/* The code below should be used when you're certain the fields in question are non-negative or, more generally, that the difference between the lowest and highest possible field values is less than or equal to Integer.MAX_VALUE(231-1).
*/
public int compareTo(PhoneNumber pn) {
// Compare area codes
int areaCodeDiff = areaCode - pn.areaCode;
if (areaCodeDiff != 0)
return areaCodeDiff;
// Area codes are equal, compare prefixes
int prefixDiff = prefix - pn.prefix;
if (prefixDiff != 0)
return prefixDiff;
// Area codes and prefixes are equal, compare line numbers
return lineNumber - pn.lineNumber;
}
Effective Java 12 Consider implementing Comparable的更多相关文章
- Effective Java 【考虑实现Comparable接口】
		
Effective Java --Comparable接口 compareTo方法是Comparable接口的唯一方法.类实现了Comparable接口,表明它的实例具有内在的排序关系. 自己实现co ...
 - effective java——12考虑实现coparable接口
		
float和double类型的主要设计目标是为了科学计算和工程计算.他们执行二进制浮点运算,这是为了在广域数值范围上提供较为精确的快速近似计算而精心设计的.然而,它们没有提供完全精确的结果,所以不应该 ...
 - Effective Java Index
		
Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...
 - 《Effective Java》读书笔记 - 3.对于所有对象都通用的方法
		
Chapter 3 Methods Common to All Objects Item 8: Obey the general contract when overriding equals 以下几 ...
 - Effective Java 第三版——12. 始终重写 toString 方法
		
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
 - Effective Java 第三版——14.考虑实现Comparable接口
		
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
 - Effective Java 第三版——34. 使用枚举类型替代整型常量
		
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
 - Effective java笔记(二),所有对象的通用方法
		
Object类的所有非final方法(equals.hashCode.toString.clone.finalize)都要遵守通用约定(general contract),否则其它依赖于这些约定的类( ...
 - Effective Java通俗理解(持续更新)
		
这篇博客是Java经典书籍<Effective Java(第二版)>的读书笔记,此书共有78条关于编写高质量Java代码的建议,我会试着逐一对其进行更为通俗易懂地讲解,故此篇博客的更新大约 ...
 
随机推荐
- IT168关于敏捷开发采访
			
1.我们知道敏捷开发是一套流程和方法的持续改进,通过快速迭代的方式交付产品,从而控制和降低成本.但是在实行敏捷初期,往往看不到很好的效果.这里面,您觉得问题主要出在哪?团队应如何去解决问题?金根:我认 ...
 - Mysql学习笔记(十三)权限管理
			
学习内容: 1.权限管理: 关于mysql的权限简单的理解就是mysql允许你做你权利以内的事情,不可以越界.比如只允许你执行select操作,那么你就不能执行update操作.只允许你从某台机器上连 ...
 - Android、iOS和Windows Phone中的推送技术
			
推送并不是什么新技术,这种技术在互联网时代就已经很流行了.只是随着进入移动互联网时代,推送技术显得更加重要.因为在智能手机中,推送从某种程度上,可以取代使用多年的短信,而且与短信相比,还可以向用户展示 ...
 - 注意力机制(Attention Mechanism)在自然语言处理中的应用
			
注意力机制(Attention Mechanism)在自然语言处理中的应用 近年来,深度学习的研究越来越深入,在各个领域也都获得了不少突破性的进展.基于注意力(attention)机制的神经网络成为了 ...
 - 【Spark】---- 在Linux集群上安装和配置Spark
			
1 安装JDK 1) 进入JDK官网 2) 下载JDK安装包 3)配置环境变量,在/etc/profile增加以下代码 JAVA_HOME=/home/hadoop/jdk1.6.0_38 PAT ...
 - Scrum项目1.0
			
1) N (Need 需求) 面向小学生 2) A (Approach 做法) 3) B (Benefit 好处) 让小学生以游戏的方式进行学习 4) C (Competitors 竞争) 减少练习 ...
 - zTree的使用
			
一.节点模糊搜索功能:搜索成功后,自动高亮显示并定位.展开搜索到的节点. 二.节点异步加载:1.点击展开时加载数据:2.选中节点时加载数据. 前台代码如下: <script type=" ...
 - 与众不同 windows phone (40) - 8.0 媒体: 音乐中心的新增功能, 图片中心的新增功能, 后台音乐播放的新增功能
			
[源码下载] 与众不同 windows phone (40) - 8.0 媒体: 音乐中心的新增功能, 图片中心的新增功能, 后台音乐播放的新增功能 作者:webabcd 介绍与众不同 windows ...
 - spring的懒加载
			
在spring的IOC容器中,可以通过设置<beans default-lazy-init="XXX"></beans>来设置是否为懒加载模式,懒加载的意思 ...
 - (旧)子数涵数·C语言——让C帮你做计算
			
之前,我们学过了我们的第一个C程序--hello World.现在开始进一步学习,想一想如何让C帮你做计算. 我们先来看代码(我没有新建,还是用之前的hello world.cpp): 好,因为之前在 ...