分享

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

 hncdman 2022-12-25 发布于湖南

 更新时间:2022年07月13日 14:08:49   作者:XH学Java  

在优先级队列中插入的元素必须能比较大小,如果不能比较大小,如插入两个学生类型的元素,会报ClassCastException异常。本文就为大家总结了Java比较两个对象大小的三种方法,需要的可以参考一下

+

目录

一. 为什么需要比较对象

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

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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);
    }
}

结果:

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

二. 元素的比较

1. 基本类型的比较 

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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. 引用类型的比较 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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()方法来进行比较内容是否相同

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

例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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));
    }
}

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

重写equals方法的步骤 

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

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

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

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

注意事项

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

2. 基于Comparable接口的比较

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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类型的对象可以插入到优先级队列中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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类型的对象也可以插入优先级队列中

3. 基于Comparator接口的比较

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

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

  • 重写compare方法 

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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类型的对象可以插入到优先级队列中

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

4. 三种比较方式对比

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

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

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

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

到此这篇关于Java比较两个对象大小的三种方法详解的文章就介绍到这了,更多相关Java比较对象大小内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多