软件编程
位置:首页>> 软件编程>> java编程>> Java比较两个对象大小的三种方法详解

Java比较两个对象大小的三种方法详解

作者:XH学Java  发布时间:2021-07-11 18:55:16 

标签:Java,比较,对象

一. 为什么需要比较对象

上一节介绍了优先级队列,在优先级队列中插入的元素必须能比较大小,如果不能比较大小,如插入两个学生类型的元素,会报ClassCastException异常

示例:

class Student{
   String name;
   int age;

public Student(String name, int age) {
       this.name = name;
       this.age = age;
   }
}
public class Test {
   public static void main(String[] args) {
       Student s1 = new Student("张三",25);
       Student s2 = new Student("李四",31);
       PriorityQueue<Student> p = new PriorityQueue<>();
       p.offer(s1);
       p.offer(s2);
   }
}

结果:

Java比较两个对象大小的三种方法详解

原因:因为优先级队列底层使用了堆数据结构,往堆中插入元素时,需要进行元素的比较,而Student是没有办法直接比较的,所以抛出异常

二. 元素的比较

1. 基本类型的比较 

Java中,基本类型的元素可以直接进行比较

public class TestCompare {
   public static void main(String[] args) {
       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);
   }
}

2. 引用类型的比较 

class Student{
   String name;
   int age;

public Student(String name, int age) {
       this.name = name;
       this.age = age;
   }
}
public class Test {
   public static void main(String[] args) {
       Student s1 = new Student("张三",25);
       Student s2 = new Student("李四",31);
       Student s3 = s1;
       System.out.println(s1==s2);  //false
       System.out.println(s1==s3);  //true
       //System.out.println(s1<s2); 编译报错
       //System.out.println(s1>s3); 编译报错
   }
}

从上述的结果来看,自定义类型不能使用>,<来比较,为什么可以使用==来比较?

==比较自定义类型时,比较的是对象的地址是否相同

但是我们往往需要比较对象的内容,如往优先级队列中插入某个对象,需要按照对象的内容来调整堆,那如何比较呢?

三. 对象比较的方法

1. equals方法比较

Object类是每一个类的基类,其提供了equals()方法来进行比较内容是否相同

Java比较两个对象大小的三种方法详解

但是Object中的equals方法默认是用==来比较的,也就是比较两个对象的地址 ,所以想让自定义类型可以比较,可以重写基类的equals()方法

例:

class Student{
   String name;
   int age;

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

@Override
   public boolean equals(Object obj) {
       if(this == obj){
           return true;
       }
       if(obj==null || !(obj instanceof Student)){
           return false;
       }
       Student s = (Student) obj;
       return this.age==s.age && this.name.equals(s.name);
   }
}
public class Test {
   public static void main(String[] args) {
       Student s1 = new Student("张三",25);
       Student s2 = new Student("李四",31);
       Student s3 = new Student("李四",31);
       System.out.println(s1.equals(s2));
       System.out.println(s2.equals(s3));
   }
}

结果:可以比较内容是否相同

Java比较两个对象大小的三种方法详解

重写equals方法的步骤 

  • 如果两个对象的地址相同,返回true

  • 如果传入的对象为null,返回false

  • 如果传入的对象与调用的对象不是同一个类型,返回false

  • 如果内容都相同则返回true,否则返回false 

注意事项

equals()方法只能比较两个对象是否相同,不能按照>,<的方式来进行比较

2. 基于Comparable接口的比较

对于引用类型,如果想按照大小的方式进行比较,在定义类时实现Comparable接口,然后在类中重写compareTo方法

例:比较两个人的大小,一般按照年龄来比较

class Person implements Comparable<Person>{
   String name;
   int age;

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

@Override
   public int compareTo(Person o) {
       if(o == null){
           return 1;
       }
       return this.age-o.age;
   }
}
public class Test1 {
   public static void main(String[] args) {
       Person p1 = new Person("小王",22);
       Person p2 = new Person("小张",21);
       Person p3 = new Person("小方",22);
       System.out.println(p1.compareTo(p2)); //>0表示大于
       System.out.println(p2.compareTo(p3)); //<0表示小于
       System.out.println(p1.compareTo(p3)); //==0表示相等
   }
}

compareTo方法是java.lang中的接口类,可以直接使用

使用Comparable接口使得Student类型的对象可以插入到优先级队列中

import java.util.PriorityQueue;

class Student implements Comparable<Student> {
   String name;
   int age;

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

@Override
   public int compareTo(Student o) {
       if(o == null){
           return -1;
       }
       return this.age-o.age;
   }
}
public class Test {
   public static void main(String[] args) {
       Student s1 = new Student("张三",25);
       Student s2 = new Student("李四",31);
       Student s3 = new Student("李四",35);
       PriorityQueue<Student> p = new PriorityQueue<>();
       p.offer(s1);
       p.offer(s2);
       p.offer(s3);
   }
}

结果:Student类型的对象也可以插入优先级队列中

Java比较两个对象大小的三种方法详解

3. 基于Comparator接口的比较

按照比较器的方式比较具体步骤如下:

  • 创建一个比较器类,实现Comparator接口

  • 重写compare方法 

使用比较器使得Student类型的对象可以插入到优先级队列中 

import java.util.Comparator;
import java.util.PriorityQueue;

class Student {
   String name;
   int age;

public Student(String name, int age) {
       this.name = name;
       this.age = age;
   }
}
class StudentComparator implements Comparator<Student>{
   @Override
   public int compare(Student o1, Student o2) {
       if(o1 == o2){
           return 0;
       }
       if(o1 == null){
           return -1;
       }
       if(o2 == null){
           return 1;
       }
       return o1.age-o2.age;
   }
}
public class Test {
   public static void main(String[] args) {
       Student s1 = new Student("张三",25);
       Student s2 = new Student("李四",31);
       Student s3 = new Student("李四",35);
       PriorityQueue<Student> p = new PriorityQueue<>(new StudentComparator());
       p.offer(s1);
       p.offer(s2);
       p.offer(s3);
   }
}

结果:Student类型的对象可以插入到优先级队列中

Java比较两个对象大小的三种方法详解

Comparator是java.util包中的泛型接口类,使用必须导入相应的包

4. 三种比较方式对比

重写的方法说明
Object.equals只能比较两个对象的内容是否相等,不能比较大小
Comparable.compareTo类要实现接口,对类的侵入性较强,破坏了原来类的结构
Comparator.compare需实现一个比较器类,对类的侵入性较弱,不破坏原来的类

Comparable,Comparator使用哪种比较方式呢?

如果拿到的是别人定义的类,我们不能对类进行操作,就选用创建类实现Comparator接口的方法

如果类是用户自己定义的类,可以对类进行操作,则采用实现Comparable接口的方法

来源:https://blog.csdn.net/qq_58710208/article/details/125725146

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com