Item 12 考虑实现Comparable接口
---Comparable接口-----
public interface Comparable<T> { int compareTo(T t); }
---Comparable接口-----
Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator.public class Employee implements Comparable<Employee> {
private int EmpID ;
private String Ename;
private double Sal ;
private static int i;
public Employee() {
EmpID = i++;
Ename = "dont know";
Sal = 0.0;
}
public Employee(String ename, double sal) {
EmpID = i++;
Ename = ename;
Sal = sal;
}
public String toString() {
return "EmpID " + EmpID + "\n" + "Ename " + Ename + "\n" + "Sal " + Sal ;
}
public int compareTo(Employee arg0) {
// TODO Auto-generated method stub
if (Sal < arg0. Sal)
return -1;
else if (Sal == arg0.Sal)
return 0;
else
return 1;
}
}
然后就可以使用该类了,结合List<T>,Array集合实现对集合中的元素进行排序,如下:
public class ComparableDemo {
public static void main(String[] args) {
List<Employee> ts1 = new ArrayList<Employee>();
ts1.add(new Employee( "Tom", 40000.00));
ts1.add(new Employee( "Harry", 20000.00));
ts1.add(new Employee( "Maggie", 50000.00));
ts1.add(new Employee( "Chris", 70000.00));
Collections.sort(ts1);
Iterator<Employee> itr = ts1.iterator();
System.out.println( "------List--------");
while (itr.hasNext()) {
Object element = itr.next();
System. out.println(element + "\n" );
}
System.out.println( "-----Array--------");
Employee[] oneArray =
{ new Employee("Peter" , 30000.00), new Employee( "Harry", 20000.00),
new Employee("Maggie" , 50000.00)};
Arrays.sort(oneArray);
for (Employee one : oneArray) {
System. out.println(one + "\n" );
}
}
}
Employee one = new Employee( "Tom", 40000.00);
Employee two = new Employee("Chris", 70000.00);
if (one.compareTo( two) == -two .compareTo(one)) {
System. out.println("satisfy rule one." );
}
Employee x = new Employee( "Tom", 40000.00);
Employee y = new Employee( "ChrisY", 30000.00);
Employee z = new Employee( "Chris", 20000.00);
if (x.compareTo(y) > 0 && y.compareTo(z) > 0 && x.compareTo(z) > 0) { System. out.println("satisfy rule No.two" ); }
Employee x = new Employee( "Tom", 40000.00);
Employee y = new Employee( "ChrisY", 40000.00);
Employee z = new Employee( "Chris", 20000.00);
if (x.compareTo(y) == 0) {
if (x.compareTo(z) == y.compareTo(z)) {
System. out.println("satisfy rule No.threee" );
}
}
public class NewEmployee implements Comparable<NewEmployee> {
public static final int Low = 10;
public static final int Middle = 20;
public static final int High = 30;
private int EmpID ;
private String Ename;
private double Sal ;
private static int i;
private Grade oneGrade;
public NewEmployee() {
EmpID = i++;
Ename = "dont know";
Sal = 0.0;
oneGrade = new Grade( Low );
}
public NewEmployee(String ename, double sal, Grade grade) {
EmpID = i++;
Ename = ename;
Sal = sal;
oneGrade = grade;
}
public String toString() {
return "EmpID " + EmpID + "\n" + "Ename " + Ename + "\n" + "Sal " + Sal + "\n" + "Grade "
+ getGradeName( oneGrade.getGrade());
}
private String getGradeName( int grade) {
switch (grade) {
case Low :
return "Low" ;
case Middle :
return "Middle" ;
case High :
return "High" ;
default:
return "Null" ;
}
}
public int compareTo(NewEmployee arg0) {
// TODO Auto-generated method stub
if ( oneGrade.compareTo(arg0. oneGrade) == 0) {
if ( Sal < arg0. Sal) {
return -1;
} else if (Sal == arg0. Sal) {
return 0;
} else {
return 1;
}
} else {
return oneGrade.compareTo(arg0. oneGrade);
}
}
}
--------
public class NewComparableDemo {
public static void main(String[] args) {
List<NewEmployee> ts1 = new ArrayList<NewEmployee>();
ts1.add( new NewEmployee( "Tom" , 40000.00, new Grade(NewEmployee. Middle)));
ts1.add( new NewEmployee( "Harry" , 20000.00, new Grade(NewEmployee. Low)));
ts1.add( new NewEmployee( "Maggie" , 50000.00, new Grade(NewEmployee. High)));
ts1.add( new NewEmployee( "Chris" , 70000.00, new Grade(NewEmployee. Low)));
Collections. sort(ts1);
Iterator<NewEmployee> itr = ts1.iterator();
while (itr.hasNext()) {
Object element = itr.next();
System. out .println(element + "\n" );
}
NewEmployee x = new NewEmployee( "Tom" , 40000.00, new Grade(NewEmployee.Middle ));
NewEmployee y = new NewEmployee( "Harry" , 20000.00, new Grade(NewEmployee.Low ));
if (x.compareTo(y) == -y.compareTo(x)) {
System. out .println("satisfy rule No.one" );
}
NewEmployee x1 = new NewEmployee( "Tom" , 40000.00, new Grade(NewEmployee.Middle ));
NewEmployee y1 = new NewEmployee( "Harry1" , 70000.00, new Grade(NewEmployee.Low ));
NewEmployee z1 = new NewEmployee( "Harry2" , 30000.00, new Grade(NewEmployee.Low ));
if (x1.compareTo(y1) > 0 && y1.compareTo(z1) > 0 && x1.compareTo(z1) > 0) {
System. out .println("satisfy rule No.two" );
}
NewEmployee x2 = new NewEmployee( "Tom2" , 70000.00, new Grade(NewEmployee.Middle ));
NewEmployee y2 = new NewEmployee( "Harry2" , 70000.00, new Grade(NewEmployee.Middle ));
NewEmployee z2 = new NewEmployee( "Harry2" , 30000.00, new Grade(NewEmployee.Low ));
if (x2.compareTo(y2) == 0) {
if (x2.compareTo(z2) == y2.compareTo(z2)) {
System. out .println("satisfy rule No.three" );
}
}
}
}
Item 12 考虑实现Comparable接口的更多相关文章
- 12.Java中Comparable接口,Readable接口和Iterable接口
1.Comparable接口 说明:可比较(可排序的) 例子:按照MyClass的y属性进行生序排序 class MyClass implements Comparable<MyClass> ...
- 第12条:考虑实现Comparable接口
CompareTo方法没有在Object中声明,它是Comparable接口中的唯一的方法,不但允许进行简单的等同性比较,而且允许执行顺序比较.类实现了Comparable接口,就表明它的实例具有内在 ...
- EffectiveJava(12)考虑实现Comparable接口
考虑实现Comparable接口 compareTo方法 Comparable接口的唯一方法,允许进行简单的等同性比较,允许执行顺序比较 Comparable接口被所有值类实现.所以如果一个值类有非常 ...
- Java6.0中Comparable接口与Comparator接口详解
Java6.0中Comparable接口与Comparator接口详解 说到现在,读者应该对Comparable接口有了大概的了解,但是为什么又要有一个Comparator接口呢?难道Java的开发者 ...
- 十三、实现Comparable接口和new Comparator<T>(){ }排序的实现过程
参考:https://www.cnblogs.com/igoodful/p/9517784.html Collections有两种比较规则方式,第一种是使用自身的比较规则: 该类必须实现Compara ...
- java实现Comparable接口和Comparator接口,并重写compareTo方法和compare方法
原文地址https://segmentfault.com/a/1190000005738975 实体类:java.lang.Comparable(接口) + comareTo(重写方法),业务排序类 ...
- Java之comparable接口
comparable 接口: 1. 问题:java.util.Collections 类中的方法 Collections.sort(List list) 是根据什么确定容器中对象的“大小”顺序的? 2 ...
- Java的Comparable接口的一个陷阱
转载自:http://my.oschina.net/jack230230/blog/56339 Java的Comparable接口提供一个对实现了这个接口的对象列表进行排序的办法.原始的排序对于简单的 ...
- 关于comparable接口
参考博客: https://blog.csdn.net/nvd11/article/details/27393445 第一个例子 @Test public void fun1(){ List list ...
随机推荐
- BluetoothServerSocket详解
一. BluetoorhServerSocket简介 1. 继承关系 public final class BluetoothServerSocket extends Object implement ...
- Python 字符串与基本语句
Python特点 python中没有变量的声明 语句结束后没有分号 严格要求缩进 支持很长很长的大数运算(直接在Idle中输入即可) 用"#"来注释 BIF:Bulit-in fu ...
- java — 设计模式
设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 一.设计模式的分类 ...
- Alpha 冲刺4
队名:日不落战队 安琪(队长) 今天完成的任务 组织第四次站立式会议. 完成40%草稿箱前端界面. 明天的计划 剩下的60%草稿箱前端界面. 如果还有时间,尝试去调用数据. 还剩下的任务 回收站前端界 ...
- 让你的SilverLight程序部署在任意服务器上
是的,即使是免费的只支持HTML的空间,同样可以部署SilverLight应用.众所周知,SilverLight的部署问题其实就是.xap文件名是否能被服务器支持的问题.解决的方法无非就是添加MIME ...
- sublime text 输入法不跟随光标
1.引子 sublime text 有个BUG,那就是不支持中文的鼠标跟随(和PS类似输入的光标和文字候选框不在一起).如下图: 2.插件 安装IMESupport插件即可插件,这款插件是日本人写的. ...
- Lucene笔记二
lucene 的排序 package cn.itcast.lucene; import java.io.IOException; import org.apache.lucene.document.D ...
- <hx>标签 字体自动加粗 自动换行
<hx>标签 字体自动加粗 自动换行
- Python os模块常用函数详解
当前使用平台: os.name #返回当前使用平台的代表字符,Windows用'nt'表示,Linux用'posix'表示 当前路径和文件 os.getcwd() #返回当前工作目录 os.listd ...
- Android 自定义View消除锯齿实现图片旋转,添加边框及文字说明
先看看图片的效果,左边是原图,右边是旋转之后的图: 之所以把这个写出来是因为在一个项目中需要用到这样的效果,我试过用FrameLayout布局如上的画面,然后旋转FrameLayout,随之而来也 ...