int pos = 0; int low = 0; int hi = l.size() - 1;    if (isSequential(l)) { ListIterator<T> itr = ((List<T>) l).listIterator(); int i = 0; T o = itr.next(); boolean forward = true; while (low <= hi) { pos = (low + hi) >> 1; if (i < pos) { if (!forward) itr.next(); for ( ; i != pos; i++, o = itr.next()); forward = true; } else { if (forward) itr.previous(); for ( ; i != pos; i--, o = itr.previous()); forward = false; } final int d = compare(key, o, c); if (d == 0) return pos; else if (d < 0) hi = pos - 1; else  low = ++pos; } } else { while (low <= hi) { pos = (low + hi) >> 1; final int d = compare(key, ((List<T>) l).get(pos), c); if (d == 0) return pos; else if (d < 0) hi = pos - 1; else  low = ++pos; } }   return -pos - 1;
return binarySearch(l, key, null);
  public static <T> int binarySearch(List<? extends T> l, T key,				     Comparator<? super T> c)  {    int pos = 0;    int low = 0;    int hi = l.size() - 1;    // We use a linear search with log(n) comparisons using an iterator    // if the list is sequential-access.    if (isSequential(l))      {	ListIterator<T> itr = ((List<T>) l).listIterator();        int i = 0;	T o = itr.next(); // Assumes list is not empty (see isSequential)	boolean forward = true;        while (low <= hi)          {            pos = (low + hi) >> 1;            if (i < pos)	      {		if (!forward)		  itr.next(); // Changing direction first.		for ( ; i != pos; i++, o = itr.next());		forward = true;	      }            else	      {		if (forward)		  itr.previous(); // Changing direction first.		for ( ; i != pos; i--, o = itr.previous());		forward = false;	      }	    final int d = compare(key, o, c);	    if (d == 0)              return pos;	    else if (d < 0)              hi = pos - 1;	    else              // This gets the insertion point right on the last loop              low = ++pos;          }      }    else      {	while (low <= hi)	  {	    pos = (low + hi) >> 1;	    final int d = compare(key, ((List<T>) l).get(pos), c);	    if (d == 0)              return pos;	    else if (d < 0)              hi = pos - 1;	    else              // This gets the insertion point right on the last loop              low = ++pos;	  }      }    // If we failed to find it, we do the same whichever search we did.    return -pos - 1;  }