Java 關於重寫compareTo方法

概述

"當一個類實現類Comparable接口,此類就可以跟很多泛型算法(generic algorithm) 以及依賴於該接口
的集合實現(Collection implementation)進行協作"

比如:字母排序,按數字排序,年代排序等等某種**定製排序**

Comparable接口

public interface Comparable<T>{
int compareTo(T t);
}

int compareTo(T t)方法說明

定義:比較此對象與指定對象的順序。
返回:負整數、零或正整數。如果該對象小於、等於或大於指定對象,則分別返回負整數、零或正整數。

升序/降序

int result = obj1.compareTo(obj2);

假如result返回1。Collections.sort(List)方法就是升序;
假如result返回-1。Collections.sort(List)方法就是降序;


代碼示例:

第一種:

對多個關鍵域,順序比較

package com.sta;

public class Student implements Comparable<Student> {
    private int age;
    private float height;
    private String name;

    ......

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + Float.floatToIntBits(height);
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
        if (obj == this) {
            return true;
        }
        if (obj != null && obj instanceof Student) {
            Student student = (Student) obj;
            return student.age == this.age && Float.floatToIntBits(student.height) == Float.floatToIntBits(this.height)
                    && (this.name == null ? student.name == null : this.name.equals(student.name));
        }
        return false;
    }

    **@Override
    public int compareTo(Student student) {
        // TODO Auto-generated method stub
        if(this.age > student.age){
            return 1;
        } 
        if(this.age < student.age){
            return -1;
        }
        if(this.height>student.height){
            return 1;
        }
        if(this.height<student.height){
            return -1;
        }
        return this.name.compareTo(student.name);
    }**
}   

第二種:

寫法如下

@Override
    public int compareTo(Student student) {
        // TODO Auto-generated method stub
        int resultAge = this.age - student.age;
        if(resultAge!=0){
            return resultAge;
        }
        float resultHeight = this.height - student.height;
        if(resultHeight != 0){
            return Float.floatToIntBits(resultHeight);
        }
        return this.name.compareTo(student.name);
    }

第二種寫法:*如果int型參數的閾值(int resultAge = this.age - student.age)小於或等於Integer.MaxVale時,
結果值:restultAge 將會溢出,並返回一個負值*.


第三種:

如果當前類某個作用域的參數類型爲引用類型
1:此引用類型可以考慮實現Comparable接口 ;
2:亦可構造一個Comparator 比較器.


如下:

package com.sta;

import java.util.Comparator;
import java.util.Date;

public class Student implements Comparable<Student> {
    private int age;
    private float height;
    private String name;
    private Info info;
    ......
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + Float.floatToIntBits(height);
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((info == null) ? 0 : info.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
        if (obj == this) {
            return true;
        }
        if (obj != null && obj instanceof Student) {
            Student student = (Student) obj;
            return student.age == this.age && Float.floatToIntBits(student.height) == Float.floatToIntBits(this.height)
                    && (this.name == null ? student.name == null : this.name.equals(student.name))
                    && (this.info == null ? student.info == null : this.info.equals(student.info));
        }
        return false;
    }

    @Override
    public int compareTo(Student student) {
        // TODO Auto-generated method stub
        if (this.age > student.age) {
            return 1;
        }
        if (this.age < student.age) {
            return -1;
        }
        if (this.height > student.height) {
            return 1;
        }
        if (this.height < student.height) {
            return -1;
        }
        Comparator<Info> comparator = new Comparator<Info>() {

            @Override
            public int compare(Info o1, Info o2) {
                // TODO Auto-generated method stub
                Date date1 = o1.getBirthday();
                Date date2 = o2.getBirthday();
                int i = date1.compareTo(date2);
                if (i != 0) {
                    return i;
                }
                String address1 = o1.getAddress();
                String address2 = o2.getAddress();
                return address1.compareTo(address2);
            }
        };
        **int resultInfo = comparator.compare(this.info, student.info);**
        if (resultInfo != 0) {
            return resultInfo;
        }
        return this.name.compareTo(student.name);
    }
}

Student 作用域之一(Info):

package com.sta;

import java.util.Date;

public class Info {
    private Date birthday;
    private String address;

    public Info() {
        super();
    }
.............


}

最後說兩句:

1:比較基本數據類型時,可以使用”<”,”>”
2:亦可使用(Boxed primitive Type)基本類型封裝類的compare方法
3:待補充…..

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章