我如何知道 class 实现 Comparable<> 的排序顺序而实际上 运行 代码?
How can I know the sort order of a class implementing Comparable<> without actually running the code?
我有以下示例,我想知道是否有一种方法可以通过查看 compareTo() 方法而不用 运行 代码并进行反复试验。
电影class:
package com.company;
public class Movie implements Comparable<Movie> {
private double rating;
private String name;
private int year;
// Used to sort movies by year
public int compareTo(Movie m)
{
if (this.year == m.year) {
return 0;
}
else if (this.year > m.year) {
return 1;
}
return -1;
}
// Constructor
public Movie(String nm, double rt, int yr)
{
this.name = nm;
this.rating = rt;
this.year = yr;
}
}
主要class:
package com.company;
import java.util.*;
public class Main {
public static void main(String[] args) {
ArrayList<Movie> list = new ArrayList<Movie>();
list.add(new Movie("Force Awakens", 8.3, 2015));
list.add(new Movie("Star Wars", 8.7, 1977));
list.add(new Movie("Empire Strikes Back", 8.8, 1980));
list.add(new Movie("Return of the Jedi", 8.4, 1983));
Collections.sort(list);
System.out.println("Movies after sorting : ");
for (Movie movie: list)
{
System.out.println(movie.getName() + " " +
movie.getRating() + " " +
movie.getYear());
}
}
}
结果:
Movies after sorting :
Star Wars 8.7 1977
Empire Strikes Back 8.8 1980
Return of the Jedi 8.4 1983
Force Awakens 8.3 2015
您可以阅读 Comparable
的 compareTo
的 Javadoc 并发现它 returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
在你的例子中,如果这个对象的年份小于另一个对象的年份,你 return 负值 (-1),如果年份相等则为 0,如果年份相等则为正值 (1)此对象的年份大于另一个对象的年份。
因此,一个 Movie
被认为是 "less than" 另一个 Movie
如果它具有较小的 year
属性.
因此您的 compareTo
将按年份升序对 Movie
进行排序,因为 Collections.sort(List<T> list)
:
Sorts the specified list into ascending order, according to the Comparable natural ordering of its elements.
我有以下示例,我想知道是否有一种方法可以通过查看 compareTo() 方法而不用 运行 代码并进行反复试验。
电影class:
package com.company;
public class Movie implements Comparable<Movie> {
private double rating;
private String name;
private int year;
// Used to sort movies by year
public int compareTo(Movie m)
{
if (this.year == m.year) {
return 0;
}
else if (this.year > m.year) {
return 1;
}
return -1;
}
// Constructor
public Movie(String nm, double rt, int yr)
{
this.name = nm;
this.rating = rt;
this.year = yr;
}
}
主要class:
package com.company;
import java.util.*;
public class Main {
public static void main(String[] args) {
ArrayList<Movie> list = new ArrayList<Movie>();
list.add(new Movie("Force Awakens", 8.3, 2015));
list.add(new Movie("Star Wars", 8.7, 1977));
list.add(new Movie("Empire Strikes Back", 8.8, 1980));
list.add(new Movie("Return of the Jedi", 8.4, 1983));
Collections.sort(list);
System.out.println("Movies after sorting : ");
for (Movie movie: list)
{
System.out.println(movie.getName() + " " +
movie.getRating() + " " +
movie.getYear());
}
}
}
结果:
Movies after sorting :
Star Wars 8.7 1977
Empire Strikes Back 8.8 1980
Return of the Jedi 8.4 1983
Force Awakens 8.3 2015
您可以阅读 Comparable
的 compareTo
的 Javadoc 并发现它 returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
在你的例子中,如果这个对象的年份小于另一个对象的年份,你 return 负值 (-1),如果年份相等则为 0,如果年份相等则为正值 (1)此对象的年份大于另一个对象的年份。
因此,一个 Movie
被认为是 "less than" 另一个 Movie
如果它具有较小的 year
属性.
因此您的 compareTo
将按年份升序对 Movie
进行排序,因为 Collections.sort(List<T> list)
:
Sorts the specified list into ascending order, according to the Comparable natural ordering of its elements.