Comparable
Comparable可以认为是一个内比较器,实现了Comparable接口的类有一个特点,就是这些类是可以和自己比较的,至于具体和另一个实现了Comparable接口的类如何比较,则依赖compareTo方法的实现,compareTo方法也被称为自然比较方法。如果开发者add进入一个Collection的对象想要Collections的sort方法帮你自动进行排序的话,那么这个对象必须实现Comparable接口。compareTo方法的返回值是int,有三种情况:
1、比较者大于被比较者(也就是compareTo方法里面的对象),那么返回正整数
2、比较者等于被比较者,那么返回0
3、比较者小于被比较者,那么返回负整数
//实现Serializable,标识该类可被序列化
//实现Comparable接口,让此类可以利用Collections.sort()进行排序
public class User<T extends User> implements Serializable,Comparable<T>{
private String name;
private int age;
private transient String address;//transient修饰,标识该类序列化时此字段不需要进行存储
public User(String name){
this.name = name;
}
public User(String name,int age,String address){
this(name);
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getAddress() {
return address;
}
public int compareTo(T o) {
int returnInt = 0;
if(age>o.getAge()){
returnInt=1;
}else if(age==o.getAge()){
returnInt=0;
}else if(age<o.getAge()){
returnInt=-1;
}
return returnInt;
}
}
public class TestComparable {
public static void main(String[] args) {
User u1 = new User("caililiang1",20,"hubei1");
User u2 = new User("caililiang2",30,"hubei2");
User u3 = new User("caililiang3",25,"hubei3");
User u4 = new User("caililiang4",28,"hubei4");
User u5 = new User("caililiang5",23,"hubei5");
List<User> list = new ArrayList<User>();
list.add(u1);list.add(u2);list.add(u3);list.add(u4);list.add(u5);
for(int i=0;i<list.size();i++){
User u =list.get(i);
System.out.println(u.getName()+"--->"+u.getAge());
}
System.out.println("排序后---------------------");
Collections.sort(list);
for(int i=0;i<list.size();i++){
User u =list.get(i);
System.out.println(u.getName()+"--->"+u.getAge());
}
}
}
结果:
caililiang1--->20
caililiang2--->30
caililiang3--->25
caililiang4--->28
caililiang5--->23
排序后---------------------
caililiang1--->20
caililiang5--->23
caililiang3--->25
caililiang4--->28
caililiang2--->30
Comparator
Comparator可以认为是是一个外比较器,个人认为有两种情况可以使用实现Comparator接口的方式:
1、一个对象不支持自己和自己比较(没有实现Comparable接口),但是又想对两个对象进行比较
2、一个对象实现了Comparable接口,但是开发者认为compareTo方法中的比较方式并不是自己想要的那种比较方式
Comparator接口里面有一个compare方法,方法有两个参数T o1和T o2,是泛型的表示方式,分别表示待比较的两个对象,方法返回值和Comparable接口一样是int,有三种情况:
1、o1大于o2,返回正整数
2、o1等于o2,返回0
3、o1小于o3,返回负整数
创建被比较的对象
public class People implements Serializable{
private String name;
private int age;
private transient String address;//transient修饰,标识该类序列化时此字段不需要进行存储
public People(String name){
this.name = name;
}
public People(String name,int age,String address){
this(name);
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getAddress() {
return address;
}
}
创建一个实现Comparator的类,定义比较规则
public class PeopleComparator<T extends People> implements Comparator<T> {
public int compare(T o1, T o2) {
int returnInt = 0;
if(o1.getAge()>o2.getAge()){
returnInt = 1;
}else if(o1.getAge()==o2.getAge()){
returnInt = 0;
}else if(o1.getAge()<o2.getAge()){
returnInt = -1;
}
return returnInt;
}
}
测试比较实例
public class TestComparator {
public static void main(String[] args) {
People u1 = new People("caililiang1",20,"hubei1");
People u2 = new People("caililiang2",30,"hubei2");
People u3 = new People("caililiang3",25,"hubei3");
People u4 = new People("caililiang4",28,"hubei4");
People u5 = new People("caililiang5",23,"hubei5");
List<People> list = new ArrayList<People>();
list.add(u1);list.add(u2);list.add(u3);list.add(u4);list.add(u5);
for(int i=0;i<list.size();i++){
People u =list.get(i);
System.out.println(u.getName()+"--->"+u.getAge());
}
System.out.println("排序后---------------------");
Collections.sort(list,new PeopleComparator());
for(int i=0;i<list.size();i++){
People u =list.get(i);
System.out.println(u.getName()+"--->"+u.getAge());
}
}
}
结果:
caililiang1--->20
caililiang2--->30
caililiang3--->25
caililiang4--->28
caililiang5--->23
排序后---------------------
caililiang1--->20
caililiang5--->23
caililiang3--->25
caililiang4--->28
caililiang2--->30
网友评论