Comparable接口
可以直接使用java.util.Arrays 类进行数组的排序操作,但对象所在的类必须实现Comparable 接口,用于指定排序接口。
Comparable 接口定义如下:
public interface Comparable<T>{
public int compareTo(T o);
}
此方法返回一个int 类型的数据,但是此int 的值只能是以下三种:
1:表示大于
-1:表示小于
0:表示相等
要求:定义一个学生类,里面有姓名、年龄、成绩三个属性,要求按成绩由高到低排序,如果成绩相等,则按照年龄由低到高排序。
import java.util.Arrays;
class Student implements Comparable<Student> { // 指定类型为Student
private String name ;
private int age ;
private float score ;
public Student(String name,int age,float score){
this.name = name ;
this.age = age ;
this.score = score ;
}
public String toString(){
return name + "\t\t" + this.age + "\t\t" + this.score ;
}
public int compareTo(Student stu){ // 覆写compareTo()方法,实现排序规则的应用
if(this.score>stu.score){
return -1 ;
}else if(this.score<stu.score){
return 1 ;
}else{
if(this.age>stu.age){
return 1 ;
}else if(this.age<stu.age){
return -1 ;
}else{
return 0 ;
}
}
}
}
public class ComparableDemo1{
public static void main(String args[]){
Student stu[] = {new Student("张三",20,90.0f),
new Student("李四",22,90.0f),new Student("王五",20,99.0f),
new Student("赵六",20,70.0f),new Student("孙七",22,100.0f)} ;
Arrays.sort(stu) ; // 进行排序操作
System.out.println("姓名" + "\t\t" + "年龄" + "\t\t" + "成绩");
for(int i=0;i<stu.length;i++){ // 循环输出数组中的内容
System.out.println(stu[i]) ;
}
}
}
输出结果:
姓名 年龄 成绩
孙七 22 100.0
王五 20 99.0
张三 20 90.0
李四 22 90.0
赵六 20 70.0
另一种比较器:Comparator
如果一个类已经开发完成,但是在此类建立的初期并没有实现Comparable接口,此时肯定是无法进行对象排序操作。为了解决这样的问题,java又定义了另一个比较器的操作接口——Comparator。
此接口定义在java.util 包中,接口定义如下:
public interface Comparator<T>{
public int compare(T o1, T o2);
boolean equals(Object obj);
}
下面定义一个自己的类,此类没有实现Comparable接口。
import java.util.* ;
//指定类型为Student
class Student{
private String name ;
private int age ;
public Student(String name,int age){
this.name = name ;
this.age = age ;
}
public boolean equals(Object obj){ // 覆写equals方法
if(this==obj){
return true ;
}
if(!(obj instanceof Student)){
return false ;
}
Student stu = (Student) obj ;
if(stu.name.equals(this.name)&&stu.age==this.age){
return true ;
}else{
return false ;
}
}
public void setName(String name){
this.name = name ;
}
public void setAge(int age){
this.age = age ;
}
public String getName(){
return this.name ;
}
public int getAge(){
return this.age ;
}
public String toString(){
return name + "\t\t" + this.age ;
}
}
//实现比较器
class StudentComparator implements Comparator<Student>{
// 因为Object类中本身已经有了equals()方法
public int compare(Student s1,Student s2){
if(s1.equals(s2)){
return 0 ;
}else if(s1.getAge()>s2.getAge()){ // 按年龄比较,由低到高
return 1 ;
}else{
return -1 ;
}
}
}
public class ComparatorDemo{
public static void main(String args[]){
Student stu[] = {new Student("张三",20),
new Student("李四",22),new Student("王五",20),
new Student("赵六",20),new Student("孙七",22)} ;
Arrays.sort(stu,new StudentComparator()) ; // 进行排序操作
System.out.println("姓名" + "\t\t" + "年龄");
for(int i=0;i<stu.length;i++){ // 循环输出数组中的内容
System.out.println(stu[i]) ;
}
}
}
输出结果:
姓名 年龄
张三 20
王五 20
赵六 20
李四 22
孙七 22
总结:在使用中,尽可以还是用Comparable ,在需要排序的类上实现好此接口,而Comparator 需要单独建立一个排序的类,这样如果有很多的话,则排序的规则类也就会非常多,操作起来比较麻烦。