java - Overriden implementations of equals() and compareTo() never got called -
i need able sort user objects based on first name , last name. overrided compareto()
method part of comparable
interface. however, when use collections.sort(users)
, users
arraylist of user
objects, overrided implementation of compareto()
never gets called. similarly, overrided equals()
check equality of various fields in user
; name, address, email address, etc. overrided implementation never called either users.remove(thisuser)
. here code overriden methods:
public class user implements comparable<user> { [other methods , 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 adding entries calling: users.add(newuser)
, works fine. example, i'll add "john doe", "bert s", "al s", , "steven x", , remain in order added them in.
calling methods: when write entries file, first sort them: collections.sort(users);
when remove user: users.remove(thisuser);
both adding, deleting, , sorting each 1 line.
why won't either of these methods called users.remove()
or collections.sort()
?
equals call if hashcode differs
please check 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; } }
Comments
Post a Comment