当前位置: 首页 > news >正文

【数据结构】对象的比较

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

目录

equals比较

 Comparble接口类的比较

基于比较器比较


equals比较

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

public class Student {public int age;public String name;public Student(int age, String name) {this.age = age;this.name = name;}@Overridepublic 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类,返回falsereturn 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;}@Overridepublic int compareTo(Object o) {Student s = (Student) o;return this.age - s.age;}}

基于比较器比较

按照比较器方式进行比较,具体步骤如下:
  • 用户自定义比较器类,实现Comparator接口

public class StudentCompare implements Comparator<Student> {

    }

注意:区分ComparableComparator 

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

总体代码:

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> {@Overridepublic 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 需要实现一个比较器对象,对待比较类的侵入性弱,但对算法代码实现侵入性强
http://www.lryc.cn/news/444509.html

相关文章:

  • 代码随想录八股训练营第四十天| C++
  • 【C++】10道经典面试题带你玩转二叉树
  • 【裸机装机系列】13.kali(ubuntu)-优化-自定义grub启动界面个性化背景
  • 数组高阶应用(C++版)
  • Spring(四)多线程+异步任务执行服务+常见的Enable注解+SpringUnit测试
  • 解析与实现二叉树
  • Java面向对象——内部类(成员内部类、静态内部类、局部内部类、匿名内部类,完整详解附有代码+案例)
  • 操作系统笔记三
  • uniapp快速入门教程,内容来源于官方文档,仅仅记录快速入门需要了解到的知识点
  • 基于微信小程序的商品展示+ssm(lw+演示+源码+运行)
  • 【Linux】常用指令(下)(内含more、less、 head、tail、date、find、grep、zip、tar以及学习笔记)
  • DesignMode__unity__抽象工厂模式在unity中的应用、用单例模式进行资源加载
  • Leetcode3289. 数字小镇中的捣蛋鬼
  • 13_Python的高阶函数
  • 清空当前机器所有Docker容器和镜像
  • FreeRTOS学习——Systick中断、SVC中断、PendSV中断
  • 汇量科技大数据面试题及参考答案
  • 移情别恋c++ ദ്ദി˶ー̀֊ー́ ) ——14.AVL树
  • Python 的数据类型与操作
  • Python燃烧废气排放推断算法模型
  • Qt中多语言的操作(以QtCreator为例)
  • 计算机毕业设计 社区医疗服务系统的设计与实现 Java实战项目 附源码+文档+视频讲解
  • html+css学习
  • 2.gitlab ce 细粒度的权限控制
  • G - Merchant Takahashi / F - Useless for LIS
  • 自然语言处理实例
  • 『功能项目』主角属性值显示【75】
  • 单片机嵌入式编程中常用技术点
  • 【毕业论文+源码】基于ASP+NET的人事管理系统
  • 计算机毕业设计 校园志愿者管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解