如何根据此代码进行排序

时间:2018-01-21 12:45:59

标签: java

我也有一个ArrayList项。我有来自Media的课程。鉴于下面的代码,我如何按持续时间对arraylist进行排序? e.g。

Collections.sort(myMedia, ?);

这是班级

import java.util.Comparator;


public abstract class Media implements Comparable<Media>{

    private int duration;
    private String title;

    private String imageFileName;

    private static String imageFileDirectory = "src/resources/";

    public Media(String name, int seconds) {
        this.title = name;
        this.duration = seconds;
        this.imageFileName = "";
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }    

    public int getDuration() {
        return duration;
    }

    public void setDuration(int d) {
        this.duration = d;
    }

    public String getImageFileName() {
        return imageFileName;
    }

    public void setImageFileName(String imageFileName) {
        this.imageFileName = imageFileName;
    }

    public static String getImageFileDirectory() {
        return imageFileDirectory;
    }

    public static void setImageFileDirectory(String imageFileDirectory) {
        Media.imageFileDirectory = imageFileDirectory;
    }

    @Override
    public String toString() {
        return  this.getTitle() 
                + ", Duration: " + this.getDuration() + "s, " +
                "Cost: " + costInPence() + "p";
    }

    public abstract int costInPence();

    @Override
    public int compareTo(Media o) {
        return this.getTitle().compareTo(o.getTitle());
    }

    public static class DurationComparator implements Comparator<Media>{
        public int compare(Media m1, Media m2) {
           return m2.getDuration() - m1.getDuration();
        }
    }

    public static class CostComparator implements Comparator<Media>{
        public int compare(Media m1, Media m2) {
           return m2.costInPence() - m1.costInPence();
        }
    }
}

1 个答案:

答案 0 :(得分:3)

Collections#sort方法有两种变体。

第一个变种linked MDN article)只接受要排序的集合。它将按自然顺序对集合的元素进行排序。因此,元素必须实现接口Comparable,从而产生compareTo方法。您的Media对象已经使用有意义的自然顺序实现此界面,即按标题进行排序:

public abstract class Media implements Comparable<Media> {
    @Override
    public int compareTo(Media o) {
        return this.getTitle().compareTo(o.getTitle());
    }
}

其他变体documentation)接受集合和Comparator对象。然后,它将根据Comparator定义的顺序对元素进行排序。您可以通过各种方式定义Comparator,因为Java 8变得非常紧凑。但首先让我们看看您已定义的Comparator,按持续时间进行排序:

public static class DurationComparator implements Comparator<Media> {
    public int compare(Media m1, Media m2) {
       return m2.getDuration() - m1.getDuration();
    }
}

因此,如果您想按标题排序,则应使用第一个变体。如果要按持续时间排序,则需要创建DurationComparator的新实例并使用第二个变体,或者使用紧凑的Java 8语句。您的CostComparator

也是如此
// Sort by title
Collections.sort(myMedia);

// Sort by duration
Collections.sort(myMedia, new DurationComparator<>());

// Sort by duration with Java 8
Collections.sort(myMedia, Comparator.comparingInt(Media::getDuration));

// Sort by cost
Collections.sort(myMedia, new CostComparator<>());

// Sort by cost with Java 8
Collections.sort(myMedia, Comparator.comparingInt(Media::costInPence));

Comparator#comparingdocumentation)方法会创建一个Comparator对象,根据给定的对给定元素进行排序。方法参考指向产生密钥的方法。

当方法返回int时,您可以选择方法Comparator#comparingdocumentation),因为int不需要加框,所以它会稍快一些然后进入Integer

请注意,由于 Java 8 List本身也提供了sort方法(documentation)。因此,您不再需要致电Collections

myMedia.sort(Comparator.comparingInt(Media::getDuration));

另请注意,Comparator现在提供了一些有用的方法(documentation),例如,首先按一个键排序,如果键相等,则按第二个键排序:

myMedia.sort(Comparator.comparingInt(Media::getDuration)
    .thenComparing(Media::costInPence));