Java比較兩個對象大小的三種方法詳解
一. 為什么需要比較對象
上一節(jié)介紹了優(yōu)先級隊列,在優(yōu)先級隊列中插入的元素必須能比較大小,如果不能比較大小,如插入兩個學(xué)生類型的元素,會報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);
}
}
結(jié)果:

原因:因為優(yōu)先級隊列底層使用了堆數(shù)據(jù)結(jié)構(gòu),往堆中插入元素時,需要進(jìn)行元素的比較,而Student是沒有辦法直接比較的,所以拋出異常
二. 元素的比較
1. 基本類型的比較
Java中,基本類型的元素可以直接進(jìn)行比較
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); 編譯報錯
}
}
從上述的結(jié)果來看,自定義類型不能使用>,<來比較,為什么可以使用==來比較?
==比較自定義類型時,比較的是對象的地址是否相同
但是我們往往需要比較對象的內(nèi)容,如往優(yōu)先級隊列中插入某個對象,需要按照對象的內(nèi)容來調(diào)整堆,那如何比較呢?
三. 對象比較的方法
1. equals方法比較
Object類是每一個類的基類,其提供了equals()方法來進(jìn)行比較內(nèi)容是否相同

但是Object中的equals方法默認(rèn)是用==來比較的,也就是比較兩個對象的地址 ,所以想讓自定義類型可以比較,可以重寫基類的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));
}
}
結(jié)果:可以比較內(nèi)容是否相同

重寫equals方法的步驟
- 如果兩個對象的地址相同,返回true
- 如果傳入的對象為null,返回false
- 如果傳入的對象與調(diào)用的對象不是同一個類型,返回false
- 如果內(nèi)容都相同則返回true,否則返回false
注意事項
equals()方法只能比較兩個對象是否相同,不能按照>,<的方式來進(jìn)行比較
2. 基于Comparable接口的比較
對于引用類型,如果想按照大小的方式進(jìn)行比較,在定義類時實現(xiàn)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類型的對象可以插入到優(yōu)先級隊列中
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);
}
}
結(jié)果:Student類型的對象也可以插入優(yōu)先級隊列中

3. 基于Comparator接口的比較
按照比較器的方式比較具體步驟如下:
- 創(chuàng)建一個比較器類,實現(xiàn)Comparator接口
- 重寫compare方法
使用比較器使得Student類型的對象可以插入到優(yōu)先級隊列中
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);
}
}
結(jié)果:Student類型的對象可以插入到優(yōu)先級隊列中

Comparator是java.util包中的泛型接口類,使用必須導(dǎo)入相應(yīng)的包
4. 三種比較方式對比
| 重寫的方法 | 說明 |
|---|---|
| Object.equals | 只能比較兩個對象的內(nèi)容是否相等,不能比較大小 |
| Comparable.compareTo | 類要實現(xiàn)接口,對類的侵入性較強(qiáng),破壞了原來類的結(jié)構(gòu) |
| Comparator.compare | 需實現(xiàn)一個比較器類,對類的侵入性較弱,不破壞原來的類 |
Comparable,Comparator使用哪種比較方式呢?
如果拿到的是別人定義的類,我們不能對類進(jìn)行操作,就選用創(chuàng)建類實現(xiàn)Comparator接口的方法
如果類是用戶自己定義的類,可以對類進(jìn)行操作,則采用實現(xiàn)Comparable接口的方法
到此這篇關(guān)于Java比較兩個對象大小的三種方法詳解的文章就介紹到這了,更多相關(guān)Java比較對象大小內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot @Async 異步任務(wù)執(zhí)行方法
本篇文章主要介紹了Spring Boot @Async 異步任務(wù)執(zhí)行方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
SpringMVC框架使用jackson封裝數(shù)據(jù)過程中遇到的問題及解決
這篇文章主要介紹了SpringMVC框架使用jackson封裝數(shù)據(jù)過程中遇到的問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
IDEA導(dǎo)入項目報錯java程序包不存在問題及解決
在IDEA導(dǎo)入項目時,若出現(xiàn)javafx包不存在錯誤,需檢查并更改為JDK1.8版本(原默認(rèn)為JDK14),同時確保模塊目錄正確、Tomcat配置無誤,并補(bǔ)全JavaFX依賴,即可解決運行問題2025-09-09
關(guān)于MyBatis 查詢數(shù)據(jù)時屬性中多對一的問題(多條數(shù)據(jù)對應(yīng)一條數(shù)據(jù))
這篇文章主要介紹了MyBatis 查詢數(shù)據(jù)時屬性中多對一的問題(多條數(shù)據(jù)對應(yīng)一條數(shù)據(jù)),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
java實現(xiàn)Google郵箱SMTP協(xié)議的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何使用java實現(xiàn)Google郵箱SMTP協(xié)議,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-06-06
SpringMVC數(shù)據(jù)頁響應(yīng)ModelAndView實現(xiàn)頁面跳轉(zhuǎn)
本文主要介紹了SpringMVC數(shù)據(jù)頁響應(yīng)ModelAndView實現(xiàn)頁面跳轉(zhuǎn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Spring Boot與Spring Security的跨域問題解決方案
跨域問題是指在Web開發(fā)中,瀏覽器出于安全考慮,限制了不同域名之間的資源訪問,本文重點給大家介紹Spring Boot與Spring Security的跨域問題解決方案,感興趣的朋友一起看看吧2023-09-09

