简介
比较Comparator 与 Comparable接口。
示例
创建一个Player 类,并加入ArrayList
public class TestLearnJava {
public static void main(String[] args) {
List<Player> footballTeam = new ArrayList<>();
Player player1 = new Player(59, "John", 20);
Player player2 = new Player(67, "Roger", 22);
Player player3 = new Player(45, "Steven", 24);
footballTeam.add(player1);
footballTeam.add(player2);
footballTeam.add(player3);
System.out.println("Before Sorting : " + footballTeam);
Collections.sort(footballTeam);
System.out.println("After Sorting : " + footballTeam);
}
}
class Player {
private int ranking;
private String name;
private int age;
public Player(int ranking, String name, int age) {
this.ranking = ranking;
this.name = name;
this.age = age;
}
public int getRanking() {
return ranking;
}
public void setRanking(int ranking) {
this.ranking = ranking;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
输出
java: 对于sort(java.util.List<com.xx.learn.Player>), 找不到合适的方法
方法 java.util.Collections.<T>sort(java.util.List<T>)不适用
(推论变量 T 具有不兼容的上限
等式约束条件:com.xx.learn.Player
下限:java.lang.Comparable<? super T>)
方法 java.util.Collections.<T>sort(java.util.List<T>,java.util.Comparator<? super T>)不适用
(无法推断类型变量 T
(实际参数列表和形式参数列表长度不同))
Comparable
正如命名所描述,Comparable是一个接口,定义一个策略用来比较相同的对象。这个被称为【自然排序】(natural ordering)。
所以,为了排序必须将类Player实现Comparable接口。
public class TestLearnJava {
public static void main(String[] args) {
List<Player> footballTeam = new ArrayList<>();
Player player1 = new Player(59, "John", 20);
Player player2 = new Player(67, "Roger", 22);
Player player3 = new Player(45, "Steven", 24);
footballTeam.add(player1);
footballTeam.add(player2);
footballTeam.add(player3);
System.out.println("Before Sorting : " + footballTeam);
Collections.sort(footballTeam);
System.out.println("After Sorting : " + footballTeam);
}
}
class Player implements Comparable<Player> {
@Override
public int compareTo(Player otherPlayer) {
return Integer.compare(getRanking(), otherPlayer.getRanking());
}
private int ranking;
private String name;
private int age;
public Player(int ranking, String name, int age) {
this.ranking = ranking;
this.name = name;
this.age = age;
}
public int getRanking() {
return ranking;
}
public void setRanking(int ranking) {
this.ranking = ranking;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
输出
Before Sorting : [com.xx.learn.Player@6d311334, com.xx.learn.Player@682a0b20, com.xx.learn.Player@3d075dc0]
After Sorting : [com.xx.learn.Player@3d075dc0, com.xx.learn.Player@6d311334, com.xx.learn.Player@682a0b20]
Comparator
Comparator接口定义了一个compare(arg1, arg2)方法。
示例
public class TestLearnJava {
public static void main(String[] args) {
List<Player> footballTeam = new ArrayList<>();
Player player1 = new Player(59, "John", 20);
Player player2 = new Player(67, "Roger", 22);
Player player3 = new Player(45, "Steven", 24);
footballTeam.add(player1);
footballTeam.add(player2);
footballTeam.add(player3);
System.out.println("Before Sorting : " + footballTeam);
PlayerRankingComparator playerComparator = new PlayerRankingComparator();
Collections.sort(footballTeam, playerComparator);
System.out.println("After Sorting : " + footballTeam);
}
class PlayerRankingComparator implements Comparator<Player> {
@Override
public int compare(Player firstPlayer, Player secondPlayer) {
return Integer.compare(firstPlayer.getRanking(), secondPlayer.getRanking());
}
}
class PlayerAgeComparator implements Comparator<Player> {
@Override
public int compare(Player firstPlayer, Player secondPlayer) {
return Integer.compare(firstPlayer.getAge(), secondPlayer.getAge());
}
}
输出
Before Sorting : [com.xx.learn.Player@6d311334, com.xx.learn.Player@682a0b20, com.xx.learn.Player@3d075dc0]
After Sorting : [com.xx.learn.Player@3d075dc0, com.xx.learn.Player@6d311334, com.xx.learn.Player@682a0b20]
Java 8 Comparator使用方式
Comparator<Player> byRanking = Comparator
.comparing(Player::getRanking);
Comparator vs Comparable比较
当不能修改类的源代码时,又需要排序,则使用Comparator,否则使用comparable
当需要不同的比较策略时,可以使用Comparator