title: Java List contains method
body: <p>I was searching through the java ArrayList source code to see what happens when I convert the list to a array. 
  I came across the method contains which I've used allot and know about and my first reaction to the method was, what sort of algorithm do the contains method use.</p>
  
  <pre><code> public boolean contains(Object o) {
     return indexOf(o) &gt;= 0;
   } 
  
   public int indexOf(Object o) {
    if (o == null) {
       for (int i = 0; i &lt; size; i++)
          if (elementData[i]==null)
            return i;
    } else {
       for (int i = 0; i &lt; size; i++)
         if (o.equals(elementData[i]))
            return i;
      }
      return -1;
    }
  </code></pre>
  
  <p>The code above extracted from the source code shows ArrayLists are using the sequential search.
  I see so many people myself included using the contains method.
  I think this is a good example where people need to know there Algorithms and Java collections. When you sit with a list which has a large number of items and you call the contains method each time when you use the application it can become a problem.</p>
  
  <p>One of the improvements I can think about is to use a binary search. 
  Only if the items are sorted.
  So you need either to order the items when they are added/queried from the database or when not using the db order with start-up of the application.</p>
  
  <p>Is it better to use a different collection for the specific scenario or do I search the ArrayList using one of the util classes?</p>