Java Collections binarySearch() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Collections

Definition

In Java, Collections.binarySearch(list, key) looks for key in a List that is already sorted in ascending order. It checks the middle element, throws away the half that cannot contain the key, and repeats, so it needs only about log2(n) comparisons.

If the key is found, it returns the key's index. If not, it returns a negative number: -(insertion point) - 1, where the insertion point is the index at which the key would have to be inserted to keep the list sorted. The result is >= 0 only when the key is present.

A static method of the java.util.Collections utility class, available since Java 1.2. Call it on the class name, as in Collections.binarySearch(...), after import java.util.Collections;. You never create a Collections object.

Syntax

Pass a sorted list and the key. Use the three-argument form when the list is sorted with a comparator.

Java
List<Integer> nums = Arrays.asList(10, 20, 30, 40);
int a = Collections.binarySearch(nums, 30);   // 2
int b = Collections.binarySearch(nums, 25);   // -3  (insertion point 2)
int insertAt = -b - 1;                        // 2
CallWhat it does
Collections.binarySearch(list, key)List sorted in natural order.
Collections.binarySearch(list, key, cmp)List sorted with the same cmp.
-(result) - 1Turns a negative result back into the insertion point.
list.indexOf(key)Works on unsorted lists, but checks elements one by one.

On an ArrayList (or any list that offers fast index access) the search takes log n steps. On a large LinkedList it uses an iterator instead, which still needs only log n comparisons but up to n steps of walking.

Library declarations
Java
// java.util.Collections
public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key)
public static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c)

Parameters

ParameterDescription
listThe list to search. It must be sorted in ascending order (by natural order or by c).
keyThe value to find.
cThe comparator the list was sorted with. null means natural order.

Return Value

Return ValueDescription
index >= 0The key was found at this index.
negativeNot found. The value is -(insertion point) - 1, so it is never 0 or positive.

Compare with Similar Methods

Other ways to find an element:

MethodHow it differs
list.indexOf(o)Linear scan; works on unsorted lists; returns -1 when missing.
Arrays.binarySearch(arr, k)The same algorithm and return rule for arrays.
indexOfSubList(src, t)Finds a whole run of elements instead of one.
sort(list)Call this first so that binarySearch gives correct answers.

Java Examples

Example 1 — Read Found Indexes and Insertion Points

Search for one value that exists and three that do not, and decode each negative result.

Java
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsBinarySearchBasic {
    public static void main(String[] args) {
        List<Integer> sorted = Arrays.asList(10, 20, 30, 40, 50);
        int[] keys = {40, 35, 5, 60};

        for (int key : keys) {
            int r = Collections.binarySearch(sorted, key);
            if (r >= 0) {
                System.out.println(key + " -> " + r + " (found)");
            } else {
                System.out.println(key + " -> " + r + " (insert at " + (-r - 1) + ")");
            }
        }
    }
}

How It Works

  1. Found. The search checks 30 at the middle, moves right, then hits 40 at index 3.
  2. 35 is missing. It belongs between 30 and 40, so the insertion point is 3 and the result is -3 - 1 = -4.
  3. Smaller than everything. 5 would go at index 0, giving -1. That is why "not found" cannot simply be -1: index 0 needs a distinct code.
  4. Larger than everything. 60 would be appended at index 5, the list size, so the result is -6.

Example 2 — Insert New Values Without Re-sorting

Use the insertion point to add each new value at the right place, and skip values already present.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsBinarySearchInsert {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>(Arrays.asList(3, 8, 15));
        int[] incoming = {10, 1, 20, 8};

        for (int v : incoming) {
            int r = Collections.binarySearch(list, v);
            if (r >= 0) {
                System.out.println(v + " already at index " + r);
            } else {
                int at = -r - 1;
                list.add(at, v);
                System.out.println("insert " + v + " at " + at + " -> " + list);
            }
        }
    }
}

How It Works

  1. 10. It falls between 8 and 15, so the result is -3 and the value is added at index 2.
  2. 1 and 20. They go to the very front (index 0) and the very end (index 5, the size at that moment).
  3. 8 is a duplicate. The search lands on 8 at index 2 and returns a non-negative result, so nothing is added.
  4. Always sorted. Because each value is inserted at its insertion point, the list never needs a full sort.

Example 3 — Search a Descending List

A list sorted high to low must be searched with the same reversed comparator. Searching it in natural order gives a wrong answer.

Java
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsBinarySearchDesc {
    public static void main(String[] args) {
        List<Integer> desc = Arrays.asList(50, 40, 30, 20, 10);

        int right = Collections.binarySearch(desc, 20, Collections.reverseOrder());
        System.out.println("With reverseOrder(): " + right);

        int wrong = Collections.binarySearch(desc, 20);
        System.out.println("Without comparator:  " + wrong);
    }
}

How It Works

  1. Matching comparator. The list is in descending order, so the search uses Collections.reverseOrder(), the order the list really has.
  2. Correct path. At the middle (30) the reversed comparator says 20 comes later, so the search moves right and finds 20 at index 3.
  3. Wrong assumption. Without the comparator, natural order says 20 is smaller than 30, so the search moves left, checks 50, runs out of candidates, and reports -1 even though 20 is in the list.

Frequently Asked Questions

The index of the key if it is found. Otherwise a negative number equal to -(insertion point) - 1.
Compute -result - 1. That is the index where the key would be inserted to keep the list sorted.
Yes. Sort it with Collections.sort first, in the same order you search with. On an unsorted list the result is unreliable.
Any one of the matching indexes. The documentation makes no promise about which one.
Because the negative value also encodes where the key belongs. -1 means "insert at index 0", -2 means "insert at index 1", and so on.

Next: checkedCollection()

Wrap a Collection so inserting the wrong element type fails immediately.

checkedCollection() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful