Why isn't Collections.binarySearch() working with this comparable?

馋奶兔 提交于 2019-12-20 03:53:23

问题


I have this Player class which implements the Comparable interface. Then I have an ArrayList of Players. I'm trying to use binarySearch() on the list of Players to find one Player, but Java is giving me a "cannot find symbol: method binarySearch(java.util.ArrayList< Player>,Player)".

This the Player class:

class Player implements Comparable {

    private String username;
    private String password;
    Statistics stats;

    //Constructor, creates a new Player with a supplied username
    Player(String name) {
        username = name;
        password = "";
        stats = new Statistics();
    }

    //Accessor method to return the username as a String
    String getName() {
        return username;
    }

    String getPassword() {
        return password;
    }

    void setPassword(String newPass) {
        password = newPass;
    }

    //Method to change the username
    void setName(String newName) {
        username = newName;
    }

    public int compareTo(Object o) {
        return username.compareTo(((Player)o).username);
    }
}

Weird thing, when I try Collections.sort() on this same list, it works.


回答1:


Use are using generics inconsistently. Take heed of the compiler warnings. Always supply generic arguments (or never supply them).

Instead of:

class Player implements Comparable {
    [...]
    public int compareTo(Object o) {

Use

class Player implements Comparable<Player> {
    [...]
    public int compareTo(Player o) {

The rules of generics are difficult enough without the complication of rare types. So, typically the language spec gives up if you mix them up.




回答2:


As long as you are implementing Comparable, you can make compareTo() consistent with equals() by also overriding equals() and hashCode(). This is particularly easy in this case, as you can simply delegate to String. Moreover, it's convenient if you ever need a Map containing instances of Player:

class Player implements Comparable<String> {

    private String username;
    private String password;

    // ...

    @Override
    public int compareTo(String name) {
        return username.compareTo(name);
    }

    @Override
    public boolean equals(Object obj) {
        return obj instanceof Player
            && username.equals(((Player)obj).username);
    }

    @Override
    public int hashCode() {
        return username.hashCode();
    }
}


来源:https://stackoverflow.com/questions/1924728/why-isnt-collections-binarysearch-working-with-this-comparable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!