Home » Java programming language

Java Collections binarySearch() Method with Example

Collections Class binarySearch() method: Here, we are going to learn about the binarySearch() method of Collections Class with its syntax and example.
Submitted by Preeti Jain, on February 03, 2020

Collections Class binarySearch() method

Syntax:

    public static int binarySearch(List l, Type key_ele);
    public static int binarySearch(List l, Type key_ele, Comparator com);
  • binarySearch() method is available in java.util package.
  • binarySearch(List l, Type key_ele) method is used to find the given object (key_ele) in the given list (l) with the help of binary search.
  • binarySearch(List l, Type key_ele, Comparator com) method is used to find the given object (key_ele) in the given list (l) and the list must be sorted based on defined Comparator object.
  • These methods may throw an exception at the time of finding the given element.
    ClassCastException: This exception may throw when the given parameter List elements that are mutually incomparable.
  • These are static methods and it is accessible with the class name and if we try to access these methods with the class object then also we will not get any error.

Parameter(s):

  • In the first case, "binarySearch(List l, Type key_ele)"
    • List l – represents the list object.
    • Type key_ele – represents the key element to be searched.
  • In the second case, "binarySearch(List l, Type key_ele, Comparator com)"
    • List l – represents the list object.
    • Type key_ele – represents the key element to be searched.
    • Comparator com – represents the Comparator object and we set the value to null that means it is natural or default order.

Return value:

In both the cases, the return type of the method is int, it returns the position of the given key_ele(key element) when it exists in the given list.

Example:

// Java program to demonstrate the example 
// of binarySearch() method of Collections

import java.util.*;

public class BinarySearchOfCollections {
    public static void main(String args[]) {
        // Instantiates an array list object
        List < Integer > arr_l = new ArrayList < Integer > ();

        // By using add() method is to add
        // objects in an array list
        arr_l.add(20);
        arr_l.add(10);
        arr_l.add(40);
        arr_l.add(30);
        arr_l.add(50);

        // Display ArrayList
        System.out.println("ArrayList: " + arr_l);

        // By using binarySearch(arr_l,30,null) method is
        // to search the the given object 30 in an arr_l
        // based on defined comparator object (null) and
        // here we are using null so list must be sorted
        // in an ascending order
        int indices = Collections.binarySearch(arr_l, 30, null);

        // Display indices
        System.out.println("Collections.binarySearch(arr_l,30,null): " + indices);

        // By using binarySearch(arr_l,30) method is
        // to search the the given object 30 in an arr_l
        // so list must be sorted in an natural or ascending order
        indices = Collections.binarySearch(arr_l, 30);

        // Display indices
        System.out.println("Collections.binarySearch(arr_l,30): " + indices);
    }
}

Output

ArrayList: [20, 10, 40, 30, 50]
Collections.binarySearch(arr_l,30,null): -3
Collections.binarySearch(arr_l,30): -3


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.