Comparable接口(二)----与Compartor的区别

1、接口Comparable的特点:

和java内置类Integer和String等类一样,一个类实现了Comparable接口重写了CompareTo方法,将这个类实例放入List中,就可以使用Collections中的sort方法进行排序。

例子:

有一个学生类包含姓名和成绩属性,将几个学生放入容器studentlist中,需要按规则对容器内的学生排序:按照名字首字母排序,如果名字相同,按照份数从高到低排序。

package Compara;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

//创建学生类,实现Comparable接口
public class Student implements Comparable<Student> {
    //定义学生类的名字name和分数mark属性
    public String name;
    public int mark;
    //定义构造方法
    public Student(String name, int mark) {
        super();
        this.name = name;
        this.mark = mark;
    }

    //此学生类由于实现了Comparable接口,必须重写comparaTo方法。
    @Override
    public int compareTo(Student anotherStudent) {
        if (this.name.compareTo(anotherStudent.name) > 0) {
            return 1;
        } else if (this.name.compareTo(anotherStudent.name) == 0) {
            if (this.mark - anotherStudent.mark > 0) {
                return 1;
            } else if (this.mark - anotherStudent.mark == 0) {
                return 0;
            } else {
                return -1;
            }
        } else {
            return -1;
        }
    }

public static void main(String[] args) {
    //new出来四个学生对象
    Student stu1=new Student("Liming",568);
    Student stu2=new Student("Liming",580);
    Student stu3=new Student("Zhenh",480);
    Student stu4=new Student("Pengzu",668);
    //new一个studentlist,将四个学生扔进去。
    List<Student> studentlist=new ArrayList<Student>();
    studentlist.add(stu1);
    studentlist.add(stu2);
    studentlist.add(stu3);
    studentlist.add(stu4);
    //条用Collections下的sort方法进行排序。
    Collections.sort(studentlist);
    //打印出排序后的情况
    for(Student stu:students){
        System.out.println(stu.name+"=>"+stu.mark);
    }
}
    
}

输出结果:

Liming=>568
Liming=>580
Pengzu=>668
Zhenh=>480

2、Comparator接口的特点:

还是上面学生的例子,要使用Comparator(比较器)来实现学生排序,需要在自己定义一个StudentComparator类,实现Comparator接口和重载compare方法,然后使用Collections.sort(studentlist,new StudentComparator())来排序。

在学生类外部自定义一个StudentComparator类,实现Comparator接口:
package Compara;

import java.util.Comparator;
//自定义一个StudentComparator类,实现Comparator接口,重载compare方法
public class StudentComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        if(o1.name.compareTo(o2.name)>0){
            return 1;
        }else if(o1.name.compareTo(o2.name)==0){
            if(o1.mark-o2.mark>0){
                return 1;
            }else if(o1.mark-o2.mark==0){
                return 0;
            }else{
                return -1;
            }
        }else{
            return -1;
        }
        
    }
学生类,此时需要使用Collections.sort(studentlist,new StudentComparator())将自定义的比较器传进去。
ackage Compara;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class Student  {
    //定义学生类的名字name和分数mark属性
    public String name;
    public int mark;
    //定义构造方法
    public Student(String name, int mark) {
        super();
        this.name = name;
        this.mark = mark;
    }

 
public static void main(String[] args) {
    //new出来四个学生对象
    Student stu1=new Student("Liming",568);
    Student stu2=new Student("Liming",580);
    Student stu3=new Student("Zhenh",480);
    Student stu4=new Student("Pengzu",668);
    //new一个list,将四个学生扔进去。
    List<Student> studentlist=new ArrayList<Student>();
    studentlist.add(stu1);
    studentlist.add(stu2);
    studentlist.add(stu3);
    studentlist.add(stu4);
    //条用Collections下的sort方法进行排序。
    Collections.sort(studentlist,new StudentComparator());
    //打印出排序后的情况
    for(Student stu:studentlist){
        System.out.println(stu.name+"=>"+stu.mark);
    }
}
    
}

可以看出,假如我们的学生类是一个java的内置类(如Integer和String等类),我们就可以不动源码的情况下,实现排序功能。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,802评论 18 399
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,386评论 11 349
  • 集合框架: 1)特点:存储对象;长度可变;存储对象的类型可不同2)Collection(1)List:有序的;元素...
    Demo_Yang阅读 1,305评论 0 4
  • 一、基本数据类型 注释 单行注释:// 区域注释:/* */ 文档注释:/** */ 数值 对于byte类型而言...
    龙猫小爷阅读 4,294评论 0 16
  • 我喜欢这里,也许,仅仅因为这里是这里而不是别处;宛如喜欢一个人,不是有什么具体理由,而是因为,你就是你。 ...
    淡淡春山阅读 631评论 16 32