java自定义比较器【Comparable接口】

Comparable接口:【内部】比较器

——在比较对象类的内部 完成了 比较规则的制定

需求:

比较类内部的属性大小

解决:

  1. 让想要比较的对象的【类】实现 Comparable 接口
  2. 实现接口后 重写comparaTo() 方法
    @Override
    public int compareTo(Object o) {
        Person person = (Person) o;
        return this.age - person.age;
    }
  1. 在重写的方法内定义比较规则

前一个数 大于 后一个数返回一个 正数

俩数相等 返回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接口:【外部】比较器

  1. 定义一个类 实现comparator接口

public class Student implements Comparable

  1. 重写compare()传递两个参数
  2. 制定比较规则
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;
    }
}
  1. 先去创建 比较规则的对象

​ 通过对象调用 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}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值