文章目录
Comparable接口:【内部】比较器
——在比较对象类的内部 完成了 比较规则的制定
需求:
比较类内部的属性大小
解决:
- 让想要比较的对象的【类】实现 Comparable 接口
- 实现接口后 重写comparaTo() 方法
@Override
public int compareTo(Object o) {
Person person = (Person) o;
return this.age - person.age;
}
- 在重写的方法内定义比较规则
前一个数 大于 后一个数返回一个 正数
俩数相等 返回0
前一个数 小于 后一个数返回一个 负数
public class Main {
public static void main(String[] args) {
Person p1 = new Person("李白", 30);
Person p2 = new Person("杜甫", 18);
int i = p1.compareTo(p2);
if (i > 0) {
System.out.println(p1.name + " > " + p2.name);
} else if (i < 0) {
System.out.println(p1.name + " < " + p2.name);
} else {
System.out.println(p1.name + " = " + p2.name);
}
}
}
内部比较器比较数组(使用了多态)——适用版
public class Sout {
public static void soutArrays(Comparable[] o) {
for (int i = 0; i < o.length - 1; i++) {
for (int j = 0; j < o.length - 1 - i; j++) {
if (o[j].compareTo(o[j + 1]) > 0) {
Comparable temp = o[j];
o[j] = o[j + 1];
o[j + 1] = temp;
}
}
}
}
}
可添加泛型
class Peaple implements Comparable<Peaple>
Comparator接口:【外部】比较器
- 定义一个类 实现comparator接口
public class Student implements Comparable
- 重写compare()传递两个参数
- 制定比较规则
package com.util;
import java.util.Comparator;
public class SoutOutside implements Comparator {
@Override
public int compare(Object o1, Object o2) {
// Object o1 = s1;
// Object o2 = s2;
//制定比较规则 年龄比较
Student o3 = (Student) o1;
Student o4 = (Student) o2;
return o3.age - o4.age;
}
}
- 先去创建 比较规则的对象
通过对象调用 compare()再传入对象
package com.util;
public class SoutTest2 {
public static void main(String[] args) {
Student s1 = new Student("小明", 18);
Student s2 = new Student("小红", 14);
SoutOutside soutOutside = new SoutOutside();
System.out.println(soutOutside.compare(s1, s2));
}
}
外部比较器比较数组(使用了多态)——适用版
Test
public static void main(String[] args) {
Student s1 = new Student("小明", 18, 98);
Student s2 = new Student("小红", 14,61);
Student s3 = new Student("小亮", 14,87);
Student s4 = new Student("小黄", 14, 78);
Student s5 = new Student("小静", 20, 99);
Student[] arr = {s1, s2, s3, s4, s5};
System.out.println("排序前");
for (Student student : arr) {
System.out.println(student);
}
System.out.println("------------------");
//可以使用匿名对象
SoutOutside soutOutside = new SoutOutside();
sout(arr, soutOutside);
System.out.println("排序后");
for (Student student : arr) {
System.out.println(student);
}
}
发现 new 一个 SoutOutside 对象,只是为了使用compare方法,所以可以使用匿名对象
~~~java
SoutOutside soutOutside = new SoutOutside();
sout(arr, soutOutside);
/*====>*/
sout(arr, new SoutOutside();
~~~
java.util 工具包
public static void sout(Object[] arr, Comparator com){
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (com.compare(arr[j], arr[j + 1]) > 0) {
Object temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
升级版compara()
要求在年龄相同的时候,按成绩排序
public int compare(Object o1, Object o2) {
Student o3 = (Student) o1;
Student o4 = (Student) o2;
if (o3.age == o4.age) {
return Double.compare(o3.score, o4.score);
}
return o3.age - o4.age;
}
输出:
排序前
Student{name=‘小明’, age=18, score=98.0}
Student{name=‘小红’, age=14, score=61.0}
Student{name=‘小亮’, age=14, score=87.0}
Student{name=‘小黄’, age=14, score=78.0}排序后
Student{name=‘小红’, age=14, score=61.0}
Student{name=‘小黄’, age=14, score=78.0}
Student{name=‘小亮’, age=14, score=87.0}
Student{name=‘小明’, age=18, score=98.0}
Student{name=‘小静’, age=20, score=99.0}