使用Java中的Comparator实现的错误

时间:2016-03-15 16:46:16

标签: java comparator

这是我对比较器的实施。

class Checker implements Comparator<Player> {
    private static final Comparator<Player> asc = Comparator
        .comparingInt(Player::score).thenComparing(Player::name);
    private static final Comparator<Player> desc = asc.reversed();

    private final boolean descending;

    public int compare(Player player1, Player player2) {
        Comparator<Player> sorter = descending ? desc : asc;
        return sorter.compare(player1, player2);
    }
}

这是我的玩家对象。

class Player
{
    String name;
    int score;
}

这就是比较器被调用的方式。提供了调用代码,我无法修改它。

Checker check = new Checker();
Arrays.sort(Player,check.desc);

这是我得到的汇编。主要类是名称Solution。第6行是首先调用Player :: score的代码行。

 Solution.java:6: error: ')' expected
        .comparingInt(Player::score).thenComparing(Player::name);
                            ^
Solution.java:6: error: illegal start of type
        .comparingInt(Player::score).thenComparing(Player::name);
                             ^
Solution.java:6: error: ';' expected
        .comparingInt(Player::score).thenComparing(Player::name);
                                   ^
Solution.java:6: error: invalid method declaration; return type required
        .comparingInt(Player::score).thenComparing(Player::name);
                                     ^
Solution.java:6: error: <identifier> expected
        .comparingInt(Player::score).thenComparing(Player::name);
                                                         ^
Solution.java:6: error: ';' expected
        .comparingInt(Player::score).thenComparing(Player::name);
                                                          ^
Solution.java:6: error: <identifier> expected
        .comparingInt(Player::score).thenComparing(Player::name);

3 个答案:

答案 0 :(得分:1)

尝试更改Checker类:​​

 public class Checker {

    public static final Comparator<Player> asc = new Comparator<Player>() {     
        public int compare(Player o1, Player o2) {
             if(o1.score != o2.score)
                 return o1.score - o2.score;
             else
                 return o1.name.compareTo(o2.name);
        }
    };

    public static final Comparator<Player> desc = new Comparator<Player>() {        
        public int compare(Player o1, Player o2) {
             if(o1.score != o2.score)
                 return o2.score - o1.score;
             else
                 return o2.name.compareTo(o1.name);
        }
    };
}

答案 1 :(得分:0)

您不能使用方法句柄语法来访问对象的属性。改为使用lambdas:

 private static final Comparator<Player> asc = Comparator
     .comparingInt((Player p) -> p.score).thenComparing((Player p) -> p.name);

答案 2 :(得分:0)

For Java 1.7

class Compare implements Comparator<Player>{
boolean descending;
@Override
public int compare(Player o1, Player o2) {
         if(o1.score < o2.score)
             return -1;
         else if(o1.score > o2.score)
             return 1;
         else
             return o1.name.compareTo(o2.name);
    }
}

class Checker{
    Comparator<Player> asc = new Compare();
    Comparator<Player> desc = Collections.reverseOrder(asc);
}

以下是您的主要方法:

    Player[] players = new Player[3];
    Checker check = new Checker();
    Arrays.sort(players,check.desc);