概述
“当一个类实现类Comparable接口,此类就可以跟很多泛型算法(generic algorithm) 以及依赖于该接口
的集合实现(Collection implementation)进行协作”
比如:字母排序,按数字排序,年代排序等等某种定制排序
Comparable接口
public interface Comparable<T>{
int compareTo(T t);
}
int compareTo(T t)方法说明
定义:比较此对象与指定对象的顺序。
返回:负整数、零或正整数。如果该对象小于、等于或大于指定对象,则分别返回负整数、零或正整数。
升序/降序
int result = obj1.compareTo(obj2);
假如result返回1。Collections.sort(List)方法就是升序;
假如result返回-1。Collections.sort(List)方法就是降序;
代码示例:
第一种:
对多个关键域,顺序比较
package com.sta;
public class Student implements Comparable<Student> {
private int age;
private float height;
private String name;
......
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + Float.floatToIntBits(height);
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (obj == this) {
return true;
}
if (obj != null && obj instanceof Student) {
Student student = (Student) obj;
return student.age == this.age && Float.floatToIntBits(student.height) == Float.floatToIntBits(this.height)
&& (this.name == null ? student.name == null : this.name.equals(student.name));
}
return false;
}
**@Override
public int compareTo(Student student) {
// TODO Auto-generated method stub
if(this.age > student.age){
return 1;
}
if(this.age < student.age){
return -1;
}
if(this.height>student.height){
return 1;
}
if(this.height<student.height){
return -1;
}
return this.name.compareTo(student.name);
}**
}
第二种:
写法如下
@Override
public int compareTo(Student student) {
// TODO Auto-generated method stub
int resultAge = this.age - student.age;
if(resultAge!=0){
return resultAge;
}
float resultHeight = this.height - student.height;
if(resultHeight != 0){
return Float.floatToIntBits(resultHeight);
}
return this.name.compareTo(student.name);
}
第二种写法:*如果int型参数的阈值(int resultAge = this.age - student.age)小于或等于Integer.MaxVale时,
结果值:restultAge 将会溢出,并返回一个负值*.
第三种:
如果当前类某个作用域的参数类型为引用类型
1:此引用类型可以考虑实现Comparable接口 ;
2:亦可构造一个Comparator 比较器.
如下:
package com.sta;
import java.util.Comparator;
import java.util.Date;
public class Student implements Comparable<Student> {
private int age;
private float height;
private String name;
private Info info;
......
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + Float.floatToIntBits(height);
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((info == null) ? 0 : info.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (obj == this) {
return true;
}
if (obj != null && obj instanceof Student) {
Student student = (Student) obj;
return student.age == this.age && Float.floatToIntBits(student.height) == Float.floatToIntBits(this.height)
&& (this.name == null ? student.name == null : this.name.equals(student.name))
&& (this.info == null ? student.info == null : this.info.equals(student.info));
}
return false;
}
@Override
public int compareTo(Student student) {
// TODO Auto-generated method stub
if (this.age > student.age) {
return 1;
}
if (this.age < student.age) {
return -1;
}
if (this.height > student.height) {
return 1;
}
if (this.height < student.height) {
return -1;
}
Comparator<Info> comparator = new Comparator<Info>() {
@Override
public int compare(Info o1, Info o2) {
// TODO Auto-generated method stub
Date date1 = o1.getBirthday();
Date date2 = o2.getBirthday();
int i = date1.compareTo(date2);
if (i != 0) {
return i;
}
String address1 = o1.getAddress();
String address2 = o2.getAddress();
return address1.compareTo(address2);
}
};
**int resultInfo = comparator.compare(this.info, student.info);**
if (resultInfo != 0) {
return resultInfo;
}
return this.name.compareTo(student.name);
}
}
Student 作用域之一(Info):
package com.sta;
import java.util.Date;
public class Info {
private Date birthday;
private String address;
public Info() {
super();
}
.............
}
最后说两句:
1:比较基本数据类型时,可以使用”<”,”>”
2:亦可使用(Boxed primitive Type)基本类型封装类的compare方法