I am trying to check wether mylist contains a given object or not, where mylist is an ArrayList of type myCustomClass.
I am trying to check wether mylist contains a given object or not, where
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you add an instance of
MyCustomClassto the list, and then check if it contains another instance ofMyCustomClass, it will always return false, unless you override theequalsmethod in your custom class. The equals method checks if another object is functionally equal to this object.Make sure to override the
hashCodemethod each time you override the equals method.hashCodeshould return the same value for two equal objects. Also, equals should be written so that it’s symmetric:a.equals(b)if and only ifb.equals(a).Check equals and hashCode in the javadoc of
java.lang.Object.