SVN600
SVN600

Reputation: 385

Overriden implementations of equals() and compareTo() never got called

I need to be able to sort User objects based on first name and last name. To do so I overrided the compareTo() method as part of the Comparable interface. However, when I use Collections.sort(users), where users is an ArrayList of User objects, my overrided implementation of compareTo() never gets called. Similarly, I overrided equals() to check for equality of various fields in User; name, address, email address, etc. This overrided implementation is never called either for users.remove(thisUser). Here is my code for the overriden methods:

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();
   }

I am adding entries by simply calling: users.add(newUser), which works fine. For example, I'll add "John Doe", "Bert S", "Al S", and "Steven X", and they remain in the order I added them in.

Calling the methods: When I write the entries back to a file, I first sort them: Collections.sort(users);

When I remove a user: users.remove(thisUser);

Both adding, deleting, and sorting are just each one line.

Why won't either of these methods get called by users.remove() or Collections.sort()?

Upvotes: 0

Views: 184

Answers (1)

Balaji Thummalapenta
Balaji Thummalapenta

Reputation: 131

equals will call only if hashCode differs

Please check this code, it's calling 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);  

Output: Comparing.....

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;
    }

}

Upvotes: 2

Related Questions