It is known that if I would like to find whether an object is in an arraylist I can either use the contains() method:
if(arraylist.contains(obj)) { // do things }
or I could use a for loop (providing that I have overloaded the equals() method):
for(Object o : arraylist) {
if(obj.equals(o)) {
// do things
}
}
As it is referred in other posts, contains() actually makes use internally of a for loop and the equals() method. Therefore, my question is: is it logical to expect contains() to take more time when the arraylist is larger?
I am asking this because in my code I use contains() in order to avoid "for loops" and therefore keep the run time low and constant, but I notice that the code runs significantly slower as the arraylist size becomes larger.
contains()will be in single line and would be more readable and easier than your for loop