Reputation: 46
How can I set the equality comparison behavior between two objects whose class is defined by me that are stored in an object of type ArrayList?
Upvotes: 0
Views: 49
Reputation: 358
For this there exists in the Java language two fundamental methods called equals (...) and hashCode () that are declared in class java.lang.Object by default and are part of the core library of Java.
You should implement these two methods in your classes that need to be compared!
The equals()
method is used to compare if one object is equal to the other.
The hashCode()
method is used to generate an ID number corresponding to the object.
See more in:https://docs.oracle.com/javase/7/docs/api/java/util/List.html
Upvotes: 2