diff --git a/classpath/java/util/Collections.java b/classpath/java/util/Collections.java index 88df95a95f..f6af29ae9b 100644 --- a/classpath/java/util/Collections.java +++ b/classpath/java/util/Collections.java @@ -148,6 +148,22 @@ public class Collections { } } + public static int binarySearch(List list, T needle) { + int left = -1, right = list.size(); + while (left + 1 < right) { + int middle = (left + right) >> 1; + int result = ((Comparable)needle).compareTo(list.get(middle)); + if (result < 0) { + right = middle; + } else if (result > 0) { + left = middle; + } else { + return middle; + } + } + return -1 - right; + } + static T[] toArray(Collection collection, T[] array) { Class c = array.getClass().getComponentType();