# 自然排序接口Comparable和比较器Comparator

java基础 专栏收录该内容
3 篇文章 0 订阅

### 对类实现整体排序须使用Comparable接口

##### Exception in thread “main” java.lang.ClassCastException: class_chapter.StudentExample cannot be cast to java.lang.Comparable

comparable是一个接口，其中compareTo()方法定义了排序规则，可以对复杂的对象进行排序。

//测试类
public class ComparableChapter {

public static void main(String[] args) {

TreeSet<StudentExample> ts=new TreeSet<StudentExample>();

StudentExample s1=new StudentExample("s001","小明","男",20,95);

StudentExample s2=new StudentExample("s002","小王","男",20,90);
StudentExample s3=new StudentExample("s003","小花","女",21,93);

for(StudentExample st1:ts) {
System.out.println(st1);
}
}

//学生类
public class StudentExample{
private String id;
private String name;
private String sex;
private int age;

public String toString() {
}

public void setId(String id) {
this.id=id;
}
public String getId() {
return this.id;
}

public void setNmae(String name) {
this.name=name;
}
public String getNmae() {
return this.name;
}

public void setSex(String sex) {
this.sex=sex;
}
public String getSex() {
return this.sex;
}

public void setAge(int age) {
this.age=age;
}
public int getAge() {
return this.age;
}

}
}

public StudentExample() {

}
public StudentExample(String id,String name,String sex,int age,double grade) {
this.id=id;
this.name=name;
this.sex=sex;
this.age=age;
}

}



// 使用Comparable接口的学生类
public class StudentExample implements Comparable{            //实现Comparable类
private String id;
private String name;
private String sex;
private int age;

public String toString() {
}

public void setId(String id) {
this.id=id;
}
public String getId() {
return this.id;
}

public void setNmae(String name) {
this.name=name;
}
public String getNmae() {
return this.name;
}

public void setSex(String sex) {
this.sex=sex;
}
public String getSex() {
return this.sex;
}

public void setAge(int age) {
this.age=age;
}
public int getAge() {
return this.age;
}

}
}

public StudentExample() {

}
public StudentExample(String id,String name,String sex,int age,double grade) {
this.id=id;
this.name=name;
this.sex=sex;
this.age=age;
}

@Override
public int compareTo(java.lang.Object o) {                                  //重写compareTo方法
// TODO Auto-generated method stub
return 1;                                                     //1表示第二个对象比前一个打，0表示其相等，-1表示比前一个小
}

}



### 对类的属性实现整体排序使用Comparator接口

Comparator是一个类，compare()方法定义复杂的排序机制，来实现排序。

//测试类
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

import class_chapter.Student;;
public class ComparatorChapter {
public static void main(String[] agrs) {

Comparator<Student> comparator=new Comparator<Student>() {

@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
int figture1=Integer.parseInt(o1.getSid().substring(1));                  //定义的学号是S001类型要比较大小截取S后面的部分，再转化未int类型
int figture2=Integer.parseInt(o2.getSid().substring(1));
int sum=figture1-figture2;
int s=sum==0? o1.compareTo(o2) : sum;                   //comparaTo()方法通过返回值来排序,三元表达式，判断学号的差值是否等于0，两种表达式。
return s;                                               //Comparator的compare()方法和compareTo()方法一样都是通过返回值来改变排序机制，1，0，-1
}                                                           //这里返回o1.compareTo(o2)式从小大排序，如果不等于0，是负数就比上一个小，排在前面，是正数就不上一个大在后面

};

Student s3=new Student("s003","小王");
Student s2=new Student("s002","小张");
Student s1=new Student("s001","小李");

TreeSet ts1=new TreeSet(comparator);         //再创建TreeSet是需要引入Comparetor对象，对TreeSet的对象排序

Iterator Iterator = ts1.iterator();
while(Iterator.hasNext()) {
System.out.println(Iterator.next());
}

}
}

//学生类
public class Student{
private String sid;
private String sname;

public String toString() {             //注意当是String类型是一定要重写tostring方法不然输出collection_and_map.Teacher@7852e922
return sid+" "+sname;
}

public void setSid(String sid) {
this.sid=sid;
}
public String getSid() {
return this.sid;
}

public void setSname(String sname) {
this.sname=sname;
}
public String getSname() {
return this.sname;
}

public void Student() {

}
public Student(String sid,String sname) {
this.setSid(sid);
this.setSname(sname);
}

public int compareTo(Student o2) {
// TODO Auto-generated method stub
return 1;                 //该方法为collection_and_map的ComparatorChapter类重写，并调用，详情见StudentExanple类
//返回1，后面比前面打排在当前对象的后面，-1表示小，0表示相等。
}
}


TreeSet<Student> ts2=new TreeSet<Student>(new Comparator<Student>() {

@Override
public int compare(Student stu1, Student stu2) {
// TODO Auto-generated method stub
int figture1=Integer.parseInt(stu1.getSid().substring(1));
int figture2=Integer.parseInt(stu2.getSid().substring(1));
int sum=figture1-figture2;
int s=sum==0? stu1.compareTo(stu2) : sum;
return s;
}

});              //完整语句要有；不然会出错


• 0
点赞
• 0
评论
• 0
收藏
• 打赏
• 扫一扫，分享海报

12-28 66

_小许_

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。