簡體   English   中英

使用給定的比較器作為參數對對象列表進行排序的類方法(Java)

[英]Class method that sort a list of objects with a comparator given as parameter (Java)

我在定義一個方法時遇到麻煩,該方法對定義了該方法的同一類的列表進行排序。

例如,使用以下屬性定義該類:

          public abstract class Licence {
            //other attributes
            protected List<People> myList;

            //Constructor and other methods

            //The method I want
            public List<People> getPeopleInOrder ( Comparator c)
              List<People> aux = new ArrayList<People>(this.myList); 
              Collections.sort(aux, c);
              return aux;

我也有這堂課

                class CompPeople implements Comparator<People>{
                public int compare( People e1, People e2) {
                // Declaration the criteria of comparison
                if ( c1 == 0) {
                     if (c2 == 0 ) return c3;
                   else return c2;
                }
               else return c1;
        }
    }

但是當我調用main方法時

               List<People> myNewList = Mylicence.getPeopleInOrder(new CompPeople());

編輯:在我試圖對一個不可修改的列表進行排序之前,我更改了它,現在它給了我一個空列表。

我知道我可以使用Collections的排序方法,但是我想要的是License類的方法,該方法使用給定的比較器對列表進行排序。 提前致謝

正如您應該能夠從注釋中猜到的那樣,問題是unmodifiableList() 對於副本,只需創建一個新的List

public List<People> getPeopleInOrder ( Comparator c)
    List<People> aux = new ArrayList<>(this.myList);
    Collections.sort(aux, c);
    return aux;
}

我認為您的比較器不正確。

讓我們定義一個People類:

public class People {
    private String name;
    private String city;
}

如果您打算使用不同的方法來比較這些對象,那么最好使用自定義比較器。 例如,這是兩個比較器,它們按一個字段對人進行排序。 我建議在People類中定義不同的比較器:

public class People {

    private String name;
    private String city;

    public static final Comparator<People> SORT_BY_NAME_ASC = (one, two) -> one.name.compareToIgnoreCase(two.name);
    public static final Comparator<People> SORT_BY_CITY_ASC = (one, two) -> one.city.compareToIgnoreCase(two.city);

}

現在,您可以使用它對List<People> peoples排序:

people.sort(People.SORT_BY_NAME_ASC); // sort by name asc
people.sort(People.SORT_BY_CITY_ASC); // sort by city asc
people.sort(People.SORT_BY_NAME_ASC.thenComparing(People.SORT_BY_CITY_ASC));    // sort by name and city asc

您的方法getPeopleInOrder()可能看起來像這樣:

public List<People> getPeopleInOrder(Comparator<People> comparator) {
    if(myList.isEmpty())
        return Collections.emptyList();

    List<People> aux = new ArrayList<>(myList);
    aux.sort(comparator);

    return aux;
}

...或使用像這樣的Streams

public List<People> getPeopleInOrder(Comparator<People> comparator) {
    return myList.stream().sorted(comparator).collect(Collectors.toList());
}

如果只需要一個比較器,則有兩種方法。

類人們實現可比

class People implements Comparable<People> {

    private String name;
    private String city;

    @Override
    public int compareTo(People people) {
        int res = name.compareToIgnoreCase(people.name);
        res = res == 0 ? city.compareToIgnoreCase(people.city) : res;
        // compare other fields you want
        return res;
    }
}

使用單個比較器

public class People {

    private String name;
    private String city;

    public static final Comparator<People> SORTY_BY_NAME_AND_CITY = (one, two) -> {
        int res = one.name.compareToIgnoreCase(two.name);
        res = res == 0 ? one.city.compareToIgnoreCase(two.city) : res;
        // compare other fields you want
        return res;
    };
}

暫無
暫無

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

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