HOWTO · Java
Collections Binary Search in Java
Learn how to use Collections.binarySearch() in Java, with examples for ascending and descending ordered lists.
On this page
Collections.binarySearch() searches a List and returns the index of a matching element. It uses binary search, which repeatedly compares the key with the middle item and narrows the remaining range. The method is useful when a list is already ordered and you need to locate a value without checking every item in sequence.
The ordering rule is essential: the list must be sorted according to the same ordering used by the search. With no comparator argument, the method uses the elements’ natural ordering. With a comparator, both the list and the search must use that comparator’s ordering. A list that does not meet this requirement can produce an arbitrary result, including an index that appears to identify a different item.
What Collections.binarySearch() does
The method compares the key with an item near the middle of the list. If they differ, it continues in the half that could contain the key. Repeating this process narrows the search range until the key is found or the range contains no match. This explains why the method can find an item without scanning the list from its first element.
Sort the list before searching
Sort the list before calling the method, and keep its order consistent while searching. For natural ordering, use a list whose elements implement Comparable and sort it in their natural order. For another ordering, sort with the same Comparator that you pass to the search. Reversing or otherwise changing the list’s order after sorting invalidates that condition.
Choose an overload
Use the natural-order overload when the elements define the desired ordering: Collections.binarySearch(List<? extends Comparable<? super T>> list, T key). Use the comparator overload when the search needs an explicit ordering: Collections.binarySearch(List<? extends T> list, T key, Comparator<? super T> c). The comparator determines how values compare; it does not sort the list for you. Collections.reverseOrder() supplies a comparator that reverses natural ordering, so a list sorted with it can be searched in descending order with that same comparator.
Search an ascending list
The following complete program searches an ascending list for a present key and a key that is absent.
Run this example:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> values = new ArrayList<>();
Collections.addAll(values, 3, 4, 5, 20, 30);
int foundIndex = Collections.binarySearch(values, 20);
int missingResult = Collections.binarySearch(values, 12);
System.out.println(foundIndex);
System.out.println(missingResult);
}
}
3
-4
Search a descending list
For descending order, sort with Collections.reverseOrder() and pass that comparator to the search. The order used to prepare the list and the order used to compare the key must match.
Run this example:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> values = new ArrayList<>();
Collections.addAll(values, 350, 90, 45, 20, 3);
int index = Collections.binarySearch(
values, 45, Collections.reverseOrder());
System.out.println(index);
}
}
2
Interpret the returned index
A nonnegative result is the index of a matching element. If the key is absent, the result is -(insertion point) - 1, where the insertion point is the position at which the key would fit while preserving the search ordering. The extra subtraction makes the encoded result negative, so it can be distinguished from a found index and decoded from the return value.
If equal elements occur more than once, the method does not guarantee which matching index it returns. Do not rely on it to find the first or last duplicate; use an additional search around the returned position if that distinction matters. In short, sort according to the intended order, select the matching overload, and interpret a negative result through the insertion-point formula.