亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

java - 如何對包含多個異構對象的列表進行排序?

java - 如何對包含多個異構對象的列表進行排序?

至尊寶的傳說 2021-12-10 15:28:44
我有一個對象列表,其中包含四個對象(員工、學生、患者、客戶)。我需要根據它們對應的 ID 按升序對這些列表進行排序。當我使用Collections.sort(list)方法時給出ClassCastException。以下是我正在使用的完整代碼...注意:我也嘗試過Comparator接口,但無法定義compare()方法內部的邏輯來對這些對象進行排序。如果列表包含兩個對象,那么在內部定義對這些對象進行排序的邏輯很容易,但是如果列表包含兩個以上的對象,那么在compare()方法內部定義排序邏輯就非常困難。任何人都可以幫助我如何縮短這份清單嗎?請修改以下代碼并提供解決方案。輸出應按 [10,20,30,40,50,60,70,90] 之類的排序順序public class Test{    public static void main(String[] args)    {        List list = new ArrayList();        list.add(new Employee(50));        list.add(new Customer(10));        list.add(new Patient(60));        list.add(new Student(90));        list.add(new Employee(20));        list.add(new Customer(40));        list.add(new Patient(70));        list.add(new Student(30));        Collections.sort(list);        System.out.println(list);    }}class Patient implements Comparable<Patient>{    int pId;    Patient(int pId)    {        this.pId = pId;    }    @Override    public int compareTo(Patient o)    {        return this.pId - o.pId;    }    @Override    public String toString()    {        return this.pId + "";    }}class Employee implements Comparable<Employee>{    int empId;    Employee(int empId)    {        this.empId = empId;    }    @Override    public int compareTo(Employee o)    {        return this.empId - o.empId;    }    @Override    public String toString()    {        return this.empId + "";    }}class Customer implements Comparable<Customer>{    int cId;    Customer(int cId)    {        this.cId = cId;    }    @Override    public int compareTo(Customer o)    {        return this.cId - o.cId;    }    @Override    public String toString()    {        return this.cId + "";    }}class Student implements Comparable<Student>{    int sId;    Student(int sId)    {        this.sId = sId;    }    @Override    public int compareTo(Student o)    {        return this.sId - o.sId;    }    @Override    public String toString()    {        return this.sId + "";    }}
查看完整描述

3 回答

?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

使用方法定義一個接口Identifiable(或超類 Person)int getId()。

使您的所有類都實現該接口(或擴展該超類)。

停止使用原始類型,因此使用 aList<Identifiable>而不是 a List。

然后使用 a 對列表進行排序Comparator<Identifiable>,可以使用Comparator.comparingInt(Identifiable::getId).

你所有的類都不應該實現 Comparable。它們的 ID 沒有定義它們的自然順序。在這個特定的用例中,您只是碰巧按 ID 對它們進行排序。因此應該使用特定的比較器。


查看完整回答
反對 回復 2021-12-10
?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

例如Person,定義一個超類,然后在id那里添加您的?;?id 邏輯的比較也應該在那里實現。


public class Person implements Comparable<Person> {


    private int id;


    // getters, setters, compareTo, etc


}

讓你所有的基類都從 Person


public class Student extends Person { ... }

public class Customer extends Person { ... }

public class Employee extends Person { ... }

public class Patient extends Person { ... }

List用術語定義您Person并對其應用排序。


public static void main(String[] args)

{

    List<Person> list = new ArrayList<>();

    list.add(new Employee(50));

    list.add(new Customer(10));

    list.add(new Patient(60));

    list.add(new Student(90));

    list.add(new Employee(20));

    list.add(new Customer(40));

    list.add(new Patient(70));

    list.add(new Student(30));


    Collections.sort(list);

    System.out.println(list);

}


查看完整回答
反對 回復 2021-12-10
?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

創建像 person 這樣具有 id 屬性的類。然后通過它擴展所有這些類。那么你只需要為 person 類編寫比較


查看完整回答
反對 回復 2021-12-10
  • 3 回答
  • 0 關注
  • 180 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號