如何用通用比较器编写一个用于排序列表的类?

时间:2016-11-20 06:22:39

标签: java generics

我在HotelData

下面提供了以下模型
public class HotelData {

    private String name;
    private String address;
    private String stars;
    private String contact;
    private String phone;
    private String uri;


    public HotelData(String name, String address, String stars, String contact, String phone, String uri) {
        this.name = name;
        this.address = address;
        this.stars = stars;
        this.contact = contact;
        this.phone = phone;
        this.uri = uri;
    }

    public HotelData() {
    }

    public String getName() {
        return name;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getStars() {
        return stars;
    }

    public void setStars(String stars) {
        this.stars = stars;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }
}

我在类中使用以下方法来排序List<HotelData>,即rows

     public class Sorter {

    public static void sortDataList(int sortValue, List<HotelData> rows) {

        switch (sortValue) {

            // sort based on the name
            case 1:
                Collections.sort(rows, new Comparator<HotelData>() {
                    @Override
                    public int compare(HotelData p1, HotelData p2) {
                        return p1.getName().compareTo(p2.getName()); // Ascending
                    }
                });
                break;

            // sort based on the hotel rating
            case 2:
                Collections.sort(rows, new Comparator<HotelData>() {
                    @Override
                    public int compare(HotelData p1, HotelData p2) {
//                        return p1.getStars().compareTo(p2.getStars()); // Ascending
                        return p2.getStars().compareTo(p1.getStars()); // Descending
                    }
                });
                break;

            default:
                System.out.println("SORTING TYPE NOT SUPPORTED");
        }
    }

}

因此,该方法主要做的是根据某些属性对List<HotelData>进行排序并返回结果。如何使类和方法通用?我还想按照IntelliJ

的建议使用Java 8进行实现

1 个答案:

答案 0 :(得分:0)

您可以使用java的多态性,如下面的步骤所示,使其成为通用的,并避免switch个案例:

(1)定义泛型Sorter类(采用任何HotelData比较器类型)

(2)定义各种Comparator

代码如下所示:

分拣机类

public class Sorter {
   public static void sortDataList(List<HotelData> rows, 
        Comparator<HotelData> comparator) {

            //Sort using the provided comparator
            Collections.sort(rows, comparator);
  }
}

HotelNameComparator类:

public class HotelNameComparator implements Comparator<HotelData> {
         @Override
         public int compare(HotelData p1, HotelData p2) {
             return p1.getName().compareTo(p2.getName()); // Ascending
         }
    }

HotelStarsComparator类:

public class HotelStarsComparator implements Comparator<HotelData> {
             @Override
            public int compare(HotelData p1, HotelData p2) {
                return p1.getStars().compareTo(p2.getStars()); // Ascending
            }
   }

<强>用法:

public static void main(String[] args) {

   //First load arraylist rows

   //Compares by HotelName
   Sorter. sortDataList(rows, new HotelNameComparator());

   //Compares by HotelStars
   Sorter. sortDataList(rows, new HotelStarsComparator());
}

注意: 如果您使用的是Java8,则无需编写此详细代码,而是可以按照以下步骤执行此操作:

rows.sort(Comparator.comparing(HotelData::getName));
rows.sort(Comparator.comparing(HotelData:: getStars));