0

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.

5
  • 2
    "I notice that the code runs significantly slower as the arraylist size becomes larger.", that makes sense, as the bigger the array is, then the more elements there is to search through. Commented Feb 17, 2014 at 14:00
  • @Vallentin That was what I was thinking too. But then, there is not a performance gain (or at least not a significant one) by using contains() instead of a manually-written for loop, right? Commented Feb 17, 2014 at 14:01
  • Definitely it would be slow with increase in the size of arraylist. Usage of contains() will be in single line and would be more readable and easier than your for loop Commented Feb 17, 2014 at 14:02
  • Perhaps a little since you'll have less overhead, but the contains() still needs to loop through the data set. Commented Feb 17, 2014 at 14:02
  • ok, it is clear now, thank you all for your answers. I was just wondering if there could possibly be any significant performance gain due to some inner-mechanic of contains() that I might was not aware of, but it seems that this is not the case! Commented Feb 17, 2014 at 14:09

5 Answers 5

6

Yes, contains() for a List takes time proportional to the size of the list. If you need to be able to check membership faster, consider using, for example, a HashSet, which can check membership in constant time.

Sign up to request clarification or add additional context in comments.

3 Comments

or a sorted arraylist with binary search
Beat me with a minute. ;)
And of course it depends on the location of the object in question. But, yes, in the worst case you have to check all the elements.
3

is it logical to expect contains() to take more time when the arraylist is larger?

yes. by your own words:

As it is referred in other posts, contains() actually makes use internally of a for loop and the equals() method.

More elements to search = more time taken.

If you need constant lookup you could try using a hashset (may or may not be applicable to your situation). Alternately, if your data is sorted you could use a binary search to go from O(n) to O(lg(N)).

Comments

1

Is it logical to expect contains() to take more time when the arraylist is larger?

Yes. The ArrayList Javadoc says:

The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking).

Thus, the complexity of ArrayList.contains() is O(n), which makes sense since you need to check each element of the ArrayList. There is no other option, since an ArrayList is not sorted and there is no other access path to a specific element, given its value.

2 Comments

"The constant factor is low compared to that for the LinkedList implementation." Does that mean that if I was using a LinkedList implementation of the List interface instead of an ArrayList the contains() method would result in nearly constant time?
No, a factor does not really matter for complexity analysis. Also, it says that the factor for ArrayList is lower, so if ArrayList is O(n) I would expect LinkedList to be something like O(2*n), which would be worse than ArrayList.
1

As many already pointed out: YES, it makes sense and can't be avoided.

If you do want to optimize for the contains()-case, consider using a HashSet rather than an ArrayList, provided you don't need to fill the collection with copies. The HashSet is a lot faster to search.

Comments

0

The contains() method will surely take time since it's inner implementation is also a for-loop just like what you gave as an example.

The more items you have in your ArrayList the longer time it takes to finish.

Here's a similar question Any big difference between using contains or loop through a list?

1 Comment

I saw that answer before but it was referring to the comparison of contains() with the foreach|iterator case and I thought that it might was a somehow different situation. As it seems, I was wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.