Java Concept Question
When it comes to collections, specifically ArrayList; why are an equals method and a compareTo method required to achieve searching and sorting of the elements of a list?
In java collections or ArrayList will store the elements as objects. so test 2 objects are equal than that particular class should implement the equal method based on their class implementation. ArrayList will use the equals method compare the equality using that method while searching for objects
to compare 2 objects which one is greater or lesser than we need to have compareTo method . in sorting we need to compare the 2 objects so ArrayList will use compareTo() for placing the objects in order
Note : If you like my answer please rate and help me it is very Imp for me
Get Answers For Free
Most questions answered within 1 hours.