• java中对象的比较


    一,基本元素的比较

        int a = 10;
        int b = 20;
        System.out.println(a > b);
        System.out.println(a < b);
        System.out.println(a == b);
        char c1 = 'A';
        char c2 = 'B';
        System.out.println(c1 > c2);
        System.out.println(c1 < c2);
        System.out.println(c1 == c2);
        boolean b1 = true;
        boolean b2 = false;
        System.out.println(b1 == b2);
        System.out.println(b1 != b2);

    二,equals进行比较

    对于用户实现自定义类型,都默认继承自Object类,而Object类中提供了equal方法,而==默认情况下调用的就是equal方法,如果直接用equals,则是看两个对象的地址是否一致,而我们想要比较的是这两个对象的内容是否一致,所以需要对equals进行重写。也可以让编译器自动生成。

    例如:

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student1 student1 = (Student1) o;
        return age == student1.age && Objects.equals(name, student1.name);
    }

    缺点:equals返回值是boolean类型的所以只能比较是否相同

    三,comparable接口

    对于用户自定义类型,如果要想按照大小与方式进行比较时:在定义类时,实现Comparble接口即可,然后在类中重写compareTo方法

    class Student1 implements Comparable{
        public String name;
        public int age; 
    @Override
        public int compareTo(Student1 o) {
            //return this.age-o.age;
            return this.name.compareTo(o.name);
        }
    }

    用法:

    Student1 student1 = new Student1("lihua", 12);
    Student1 student11 = new Student1("zhangsan", 14);
    System.out.println(student1.compareTo(student11));

    当返回值大于0时,student1大于student11

    等于0时,student1等于student11

    小于0时:student1小于student11

    缺点:将比较的方式写死了,不能灵活的用其他方式进行比较

    四,比较器

    1,用户自定义比较器类,实现Comparator接口

    2,覆写Comparator中的compare方法

    class AgeComparator implements Comparator{
        @Override
        public int compare(Student1 o1, Student1 o2) {
            return o1.age-o2.age;
        }
    }
    class NameComparator implements Comparator{
        @Override
        public int compare(Student1 o1, Student1 o2) {
            return o1.name.compareTo(o2.name);
        }
    }
        Student1 student1 = new Student1("lihua", 12);
        Student1 student11 = new Student1("zhangsan", 14);
        AgeComparator ageComparator=new AgeComparator();
        NameComparator nameComparator=new NameComparator();
        System.out.println(ageComparator.compare(student1, student11));
        System.out.println(nameComparator.compare(student1, student11));
    
    

    五,集合框架中PriorityQueue的比较方式

    内部定义的比较器对象,用来接收用户实例化PriorityQueue对象时提供的比较器对象

    传入比较器,此时内部定义的比较器会对我们提供的比较器进行接收

    如果没有提供比较器时:则使用默认的内部比较,将comparator置为null

    如果用户没有提供比较器对象,采用Comparable进行比较,如果优先级队列中的元素为基本元素,则可以进行比较,如果为对象,则会抛出ClassCastException异常

  • 相关阅读:
    Postgres 常用命令/脚本 (运维版)
    CAD Exchanger SDK 3.23.0 的亮点
    【GO语言基础】控制流
    docker (八)-dockerfile制作镜像
    Ajax异步请求(不等待,继续执行)
    黄菊华老师,Python毕业设计毕设辅导教程(4):Python 基础概念
    E2. Unforgivable Curse (hard version)
    Ubuntu 22.04 x86_64 源码编译 pytorch-v2.0.1 笔记【2】编译成功
    【k8s】Unable to restart cluster, will reset it: apiserver healthz异常
    【Python】【源码】SocketIO做一个网络聊天室软件-服务端源码
  • 原文地址:https://blog.csdn.net/2302_81705247/article/details/140405030