从不调用equals()和compareTo()的Overriden实现

时间:2016-07-11 13:23:45

标签: java override comparable

我需要能够根据名字和姓氏对User对象进行排序。为此,我将if (val occurs 3 times or more) do 方法作为compareTo()接口的一部分进行了覆盖。但是,当我使用Comparable时,Collections.sort(users)users个对象的ArrayList,我的User的重写实现永远不会被调用。同样,我覆盖compareTo()以检查equals()中各个字段的相等性。姓名,地址,电子邮件地址等。User永远不会调用此覆盖的实现。这是我的覆盖方法的代码:

users.remove(thisUser)

我通过简单地调用public class User implements Comparable<User> { [other methods and fields go here] @Override public int compareTo(User aUser) { System.out.println("Comparing....."); return (this.lastName.compareTo(aUser.getLastName()) < 0 ? -1 : this.lastName.compareTo(aUser.getLastName()) > 0 ? 1 : this.firstName.compareTo(aUser.getFirstName()) < 0 ? -1 : this.firstName.compareTo(aUser.getFirstName()) > 0 ? 1 : 0); } @Override public boolean equals(Object o) { if(this.firstName == ((User) o).getFirstName() && this.lastName == ((User) o).getLastName() && this.email == ((User) o).getEmail() && this.address == ((User) o).getAddress() && this.gender == ((User) o).getGender()) { return true; } return false; } @Override public int hashCode() { return super.hashCode(); } 来添加条目,这可以正常工作。 例如,我将添加&#34; John Doe&#34;,&#34; Bert S&#34;,&#34; Al S&#34;和&#34; Steven X&#34;,它们保持在我添加它们的顺序中。

调用方法: 当我将条目写回文件时,我首先对它们进行排序:      users.add(newUser)

删除用户时:      Collections.sort(users);

添加,删除和排序都只是每一行。

为什么这些方法会被users.remove(thisUser);users.remove()调用?

1 个答案:

答案 0 :(得分:2)

只有当hashCode不同时,

equals才会调用

请检查此代码,它正在调用compareTo,

 User  user1 = new User();
        user1.setFirstName("testFirst");
        user1.setLastName("testLast");
        User  user2 = new User();
        user2.setFirstName("testFirst1");
        user2.setLastName("testLast1");
        List<User> list = Arrays.asList(user1, user2);
        Collections.sort(list);  

输出:    比较.....

public class User implements Comparable<User> {
    private String lastName;
    private String firstName;

    @Override
    public int hashCode() {
        System.out.println("hashCode.....");
        int result = lastName.hashCode();
        result = 31 * result + firstName.hashCode();
        return result;
    }

    @Override
    public int compareTo(User aUser) {
        System.out.println("Comparing.....");
        return (this.lastName.compareTo(aUser.getLastName()) < 0 ? -1 :
                this.lastName.compareTo(aUser.getLastName()) > 0 ? 1 :
                        this.firstName.compareTo(aUser.getFirstName()) < 0 ? -1 :
                                this.firstName.compareTo(aUser.getFirstName()) > 0 ? 1 : 0);
    }

    @Override
    public boolean equals(Object o) {
        System.out.println("equals.....");
        if (this.firstName == ((User) o).getFirstName() &&
                this.lastName == ((User) o).getLastName()) {
            return true;
        }
        return false;
    }



    public String getFirstName(){
        return firstName;
    }
    public String getLastName(){
        return lastName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}