Is there a way to temporarily override hashcode and equals in java ?
I have a User
class with id, name
and age
attributes. The current equals
compares the id, rightly so. So if two ids, are same then the Users are equal.
Now, I have two Lists of User
and I want to find users with common names.
Other than looping every element of list1 over every element of list2, I have no other option (java 7). Is there a way, I can temporarily change the behaviour of equals
? In this case, where I am coding the logic for searching users with common names, I want to be able to say if name is same then the users are equal (even though its technically incorrect), without touching the actual User
class's equal.