【数据结构】对象的比较

Java数据类型分为基本数据类型和引用类型,基本数据类型可以直接比较大小,对于引用类型的变量不能直接比较。下面来讲解Java对象的比较。

目录

equals比较

Comparble接口类的比较

基于比较器比较


equals比较

equals是Object类中的方法,只能判断引用类型。默认判断的是地址是否相等,子类中往往重写该方法,用于判断内容是否相等。

java 复制代码
public class Student {

    public int age;
    public String name;
    
    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public boolean equals(Object obj) {
//将自己与自己比较,地址相同内容一定相同
        if(this==obj)
            return true;
//判断是否为Student类
        if(obj instanceof Student) {
//向下转型
            Student s = (Student) obj;
            return s.age==this.age&&this.name.equals(s.name);
        }else
//不是Student类,返回false
            return false;
    }
}
public class Test {
    public static void main(String[] args) {
        Student student1=new Student(18,"xiaohei");
        Student student2=new Student(18,"xpaohei");

        System.out.println(student1.equals(student2));
    }
}
  1. 如果指向同一个对象,返回 true
  2. 如果传入的对象类型不是 Student ,返回 false
  3. 按照类的实现目标完成比较,例如这里只要年龄和名字一样,学生信息就相同
  4. 注意下调用其他引用类型的比较也需要 equals ,例如这里的 name 的比较

equals与==区分点

==是一个比较运算符

  • 既可以判断基本类型,又可以判断引用类型
  • 如果判断类型为基本类型,判断的值是否相等
  • 如果判断的是引用类型,判断的是地址是否相等,即是不是同一个对象

Comparble****接口类的比较

实现Comparble接口 可用于比较用户自定义类型,并重写compareTo方法

复制代码
public class Student implements Comparable {

    public int age;
    public String name;

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public int compareTo(Object o) {
        Student s = (Student) o;
        return this.age - s.age;
    }

}

基于比较器比较

按照比较器方式进行比较,具体步骤如下:

  • 用户自定义比较器类,实现Comparator接口
复制代码
public class StudentCompare implements Comparator<Student> {

}

注意:区分Comparable和****Comparator

  • 覆写Comparator中的compare方法
复制代码
public class StudentCompare implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}

总体代码:

java 复制代码
public class Student {

    public int age;
    public String name;

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

}

public class StudentCompare implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}

public class Test {
    public static void main(String[] args) {
        Student student1=new Student(18,"aiaohei");
        Student student2=new Student(19,"aiaohei");

        StudentCompare s=new StudentCompare();
        
        System.out.println(s.compare(student1, student2));
    }
}

Comparable.compareTo与Comparator.compare区分

  • Comparable.compareTo 需要手动实现接口,侵入性比较强,但一旦实现,每次用该类都有顺序,属于内部顺序
  • Comparator.compare 需要实现一个比较器对象,对待比较类的侵入性弱,但对算法代码实现侵入性强
相关推荐
惊鸿一博28 分钟前
图标加载方式_zeroIcon_是否加前缀mdi
开发语言·前端·javascript
王八八。31 分钟前
linux后台java、postSQL部署命令
java·linux·运维
森G39 分钟前
TypeScript 基础类型
开发语言·typescript
月落归舟1 小时前
MyBatis缓存机制
java·缓存·mybatis
huipeng9261 小时前
企业级微服务开发实战(一):项目启动与工程化设计
java·开发语言·spring boot·spring cloud·微服务·云原生·架构
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ2 小时前
java实现excel导入、下载模板方法
java·开发语言·excel
眠りたいです2 小时前
现代C++:C++14中的新语言特性和库特性
c语言·开发语言·c++
段ヤシ.3 小时前
回顾Java知识点,面试题汇总Day12(持续更新)
java·mybatis
java1234_小锋3 小时前
Spring AI 2.0 开发Java Agent智能体 - MCP(模型上下文协议)
java·人工智能·spring·spring ai
seven97_top3 小时前
两小时入门Sentinel
java·sentinel