簡體   English   中英

在Java中的列表的包裝類上使用比較器強加順序(不進行排序)

[英]impose order (not sorting) using comparator on a wrapper class of a list in Java

我有一堂課,一個人。 它具有年齡,名稱,身高等。我正在創建一個名為PersonCollection的類,該類是列表的包裝(ArrayList)。

我希望能夠使用PersonCollection類來比較Person對象,這意味着,我不想使Person類實現Comparable接口,而是希望PersonCollection實現Comparator接口。

我這樣做很麻煩。 我已經實現了compare方法,但是當我比較Person Objects時仍然無法正常工作。

例如這段代碼給我一個錯誤(people is ArrayList

public void insert (Person p){
    for(int i = 0; i < people.size(); i++){
        if (people.get(i) > p){
            //Do something
        }
    }
} 

我知道如何使用Comparator進行排序,這是不同的。 我完全知道其他可能的甚至更好的解決方案(任何優先級隊列類或某種sortedset類)

出於特定原因,我希望針對ArrayList進行此操作,請您基於此解決方案,而不是建議其他數據結構。

您可以編寫一個自定義的Comparator並使用compare(a, b)方法。

https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#compare-TT-

您的代碼看起來像

if (myComparator.compare(people.get(i), p) > 0 ) {

根據您的描述,您有一個類似Wrapper的類?

public class People implements List<Person>, Comparator<Person>{
    /* methods */
}

因此,如果您想真正使用比較器接口,則必須這樣做:

public void insert (Person p){
    for(int i = 0; i < people.size(); i++){
        if (people.compare(people.get(i),p)){ // because people implements Comparator
            //Do something
        }
    }
} 

應該 (雖然不太確定)工作。

但是我強烈建議不要使用它,並且要考慮一些更好的事情,因為一個類不應是比較器和列表(因為出於完全不同的原因應同時使用兩個接口)。

更好的方法是使Person實現Comparable ,然后根據它進行排序

下面是一段代碼,您可以在其中看到自定義比較器對Person對象的age屬性進行年齡比較。

   public class TestCompare {

    public static void main(String[] args) {
        Person person1 = new Person(45, "Tom");
        Person person2 = new Person(12, "Sarah");
        Person person3 = new Person(34, "Michael");
        Person person4 = new Person(33, "Donald");
        Person person5 = new Person(65, "timothy");
        List<Person> people = new ArrayList<Person>();
        people.add(person1);
        people.add(person2);
        people.add(person3);
        people.add(person4);
        people.add(person5);

        CustomComparator comparator=new CustomComparator();

        for (Person p : people) {
            System.out.println(comparator.compare(p, new Person(55, "James")));
        }
    }

}

class CustomComparator implements Comparator<Person> {



    @Override
    public int compare(Person o1, Person o2) {
        // TODO Auto-generated method stub
        return o1.getAge().compareTo(o2.getAge());
    }


}

class Person implements Comparable<Person> {

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

    private Integer age;
    private String name;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;

    }

    @Override
    public int compareTo(Person o) {
        // TODO Auto-generated method stub
        return this.getAge().compareTo(o.getAge());
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM