Comparable是一个内部比较器,定义在实体类的内部,实现Comparable需要**重写compareTo(T o)**方法,this.属性和o.属性相比较,返回一个int类型的值,若返回值大于0,为正序,小于0为逆序。
package com.gaj.day06;
import java.util.Arrays;
/**
* 实现Comparable接口重写compareTo()方法
* 内部比较器,将比较器定义在实体类的内部
* @author Jan
*
*/
class Student implements Comparable<Student>{
private int no;
private String name;
private double score;
public Student(int no, String name, double score) {
this.no = no;
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Student [no=" + no + ", name=" + name + ", Score=" + score + "]";
}
@Override
public int compareTo(Student o) {
/*
// 方式一:
if(this.score - o.score > 0){
return 1;
}else if(this.score - o.score < 0){
return -1;
}else{
return 0;
}
*/
// 方式二:
return (int) (this.score - o.score);
}
}
public class Demo1 {
public static void main(String[] args) {
Student tom = new Student(2, "Tom", 88.6);
Student jack = new Student(1, "Jack", 90.5);
Student ani = new Student(3, "Ani", 77.5);
// 对象数组
Student[] students = {
tom, jack, ani};
// 排序
Arrays.sort(students);
for (Student student : students) {
System.out.println(student);
}
}
}
Comparator是一个外部比较器,定义在实体类的外部,实现Comparator需要**重写compare(T o1, T o2)**方法,o1.属性和o2.属性相比较,返回一个int类型的值,若返回值大于0,为正序,小于0为逆序。
package com.gaj.day06;
import java.util.Arrays;
import java.util.Comparator;
/**
* 实现Comparator接口重写compare()方法
* 外部比较器,将比较器定义在实体类的外部
* @author Jan
*
*/
class Teacher{
private int no;
private String name;
private int age;
public Teacher(int no, String name, int age) {
this.no = no;
this.name = name;
this.age = age;
}
public int getAge(){
return age;
}
@Override
public String toString() {
return "Teacher [no=" + no + ", name=" + name + ", age=" + age + "]";
}
}
// 定义Comparator类
class MyComparator implements Comparator<Teacher>{
@Override
public int compare(Teacher o1, Teacher o2) {
/*
// 方式一:
if(o1.getAge() - o2.getAge() > 0){
return 1;
}else if(o1.getAge() - o2.getAge() < 0){
return -1;
}else{
return 0;
}
*/
// 方式二:
return o1.getAge() - o2.getAge();
}
}
public class Demo2 {
public static void main(String[] args) {
Teacher wang = new Teacher(2, "王老师", 27);
Teacher li = new Teacher(3, "李老师", 33);
Teacher liu = new Teacher(1, "刘老师", 26);
Teacher[] teachers = new Teacher[]{
wang, li, liu};
// 方式一: 普通定义
// MyComparator myComparator = new MyComparator();
// Arrays.sort(teachers, myComparator);
// 方式二:匿名内部类
/*
Arrays.sort(teachers, new Comparator() {
@Override
public int compare(Teacher o1, Teacher o2) {
return o1.getAge() - o2.getAge();
}
});
*/
// 方式三:Lambda表达式
// 因为comparator是一个函数式接口因此可以使用lambda表达式简化
Arrays.sort(teachers,(t1,t2) -> t1.getAge() - t2.getAge());
// 展示
for (Teacher teacher : teachers) {
System.out.println(teacher);
}
}
}
// 接收一个实体类数组,但实体类的排序还是需要在实体类的内部实现
public static void sort(Object[] a) {
if (LegacyMergeSort.userRequested)
legacyMergeSort(a);
else
ComparableTimSort.sort(a, 0, a.length, null, 0, 0);
}
// 第一个参数接收一个实体类数组,第二个参数接收外部比较器
public static <T> void sort(T[] a, Comparator<? super T> c) {
if (c == null) {
sort(a);
} else {
if (LegacyMergeSort.userRequested)
legacyMergeSort(a, c);
else
TimSort.sort(a, 0, a.length, c, null, 0, 0);
}
}
// 接收一个List集合,list集合的元素如果是实体类需要自己定义内部比较器实现排序
public static <T extends Comparable<? super T>> void sort(List<T> list) {
list.sort(null);
}
// 第一个参数接收一个List集合,第二个参数接收一个外部比较器
public static <T> void sort(List<T> list, Comparator<? super T> c) {
list.sort(c);
}