code: public static <T>
      int binarySearch(List<? extends Comparable<? super T>> list, T key) {
          if (list instanceof RandomAccess || list.size()<BINARYSEARCH_THRESHOLD)
              return Collections.indexedBinarySearch(list, key);
          else
              return Collections.iteratorBinarySearch(list, key);
      }
docs: Searches the specified list for the specified object using the binary
  search algorithm.  The list must be sorted into ascending order
  according to the {@linkplain Comparable natural ordering} of its
  elements (as by the {@link #sort(List)} method) prior to making this
  call.  If it is not sorted, the results are undefined.  If the list
  contains multiple elements equal to the specified object, there is no
  guarantee which one will be found.
  
  <p>This method runs in log(n) time for a "random access" list (which
  provides near-constant-time positional access).  If the specified list
  does not implement the {@link RandomAccess} interface and is large,
  this method will do an iterator-based binary search that performs
  O(n) link traversals and O(log n) element comparisons.
  
  @param  <T> the class of the objects in the list
  @param  list the list to be searched.
  @param  key the key to be searched for.
  @return the index of the search key, if it is contained in the list;
  otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
  <i>insertion point</i> is defined as the point at which the
  key would be inserted into the list: the index of the first
  element greater than the key, or <tt>list.size()</tt> if all
  elements in the list are less than the specified key.  Note
  that this guarantees that the return value will be &gt;= 0 if
  and only if the key is found.
  @throws ClassCastException if the list contains elements that are not
  <i>mutually comparable</i> (for example, strings and
  integers), or the search key is not mutually comparable
  with the elements of the list.