Open In App

Binary Search vs contains Performance in Java List

Improve
Improve
Like Article
Like
Save
Share
Report

Java provides two methods namely Collections.binarySearch() and contains() to find an element inside a list. Underneath the hood, contains() method uses indexOf() method to search for the element. indexOf() method linearly loops through the List and compares every element with the key until the key is found and returns true otherwise it returns false when the element is not found. so, the time complexity of contains() is O(n). The time complexity of Collections.binarySearch() is O(log2(n)). But if we want to use this method then the list should be sorted. If the list is not sorted then we need to sort it before using Collections.binarySearch() which takes O(nlog(n)) time.

How to choose:

  • If the element to be found is near the starting of the list then the performance of contains() method is better as contains() start searching for the element from the starting of the list linearly.
  • If the elements are sorted and the number of elements is relatively large then Collections.binarySearch() is faster as it only takes O(log2(n)) time.
  • If the elements of the list are unsorted then the performance of contains() method is better as it only takes O(n) time but if the number of search queries is high then the overall performance of Collections.binarySearch() method is better as we sort list only once during the first search which takes O(nlog(n)) time and after that each search operations takes O(log(n)) time.
  • For a list that contains a relatively small number of elements then contains() yields better speed.
  • If we are using a LinkedList that does not implement the RandomAccess interface and hence is unable to provide O(1) time to access an element, then we should prefer contains() over Collections.binarySearch() as Collections.binary search() takes O(n) to perform link traversals, and then it takes O(log(n)) time to perform comparisons.

Now we will be discussing out two variants where a sorted List is

  1. Sorted small List
  2. Sorted large List
  3. Unsorted List

Case 1: For a small sorted list

In the code mentioned below, we have taken the example of a sorted list that contains only 100 elements from 0 to 99 and we search for 40 and as we have seen above that in small lists contains() has an edge over Collections.binarySearch when it comes to speed.

Example 

Java




// Java program to compare the performance
// of contains() and Collections.binarySearch()
// For a Small List (Case 1)
 
// Importing ArrayList and Collections classes
// from java.util package
import java.util.ArrayList;
import java.util.Collections;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of ArrayList
        // Declaring object of integer type
        ArrayList<Integer> arr = new ArrayList<>();
 
        // Iterating over object using for loop
        for (int i = 0; i < 100; i++) {
            arr.add(i);
        }
 
        // Calculating and printing the time taken
        // where we are finding 40
        // Using contains() method
        long start = System.nanoTime();
        arr.contains(40);
        long end = System.nanoTime();
 
        // Print statement
        System.out.println(
            "Time taken to find 40 inside arr using contains() = "
            + (end - start) + " nano seconds");
 
        // Calculating and printing the time taken
        // to find 40
        // Using Collections.binarySearch() method
        start = System.nanoTime();
        Collections.binarySearch(arr, 40);
        end = System.nanoTime();
 
        // Print statement
        System.out.println(
            "Time taken to find 40 inside arr using binarySearch() = "
            + (end - start) + " nano seconds");
    }
}


Output

Time taken to find 40 inside arr using contains() = 16286 nano seconds
Time taken to find 40 inside arr using binarySearch() = 87957 nano seconds

Case 2: For a large sorted list

In the mentioned below, we have created a sorted ArrayList which contains 100000 elements from 0 to 99999, and we find the element 40000 inside it using contains() and Collections.sort() method. As the list is sorted and has a relatively large number of elements the performance of Collections.sort() is better than contains() method.

Example

Java




// Java program to Find and Compare the Performance
// of contains() and Collections.sort() Methods
// For Large Sorted ArrayList (Case 2)
 
// Importing ArrayList and Collections classes
// from java.util package
import java.util.ArrayList;
import java.util.Collections;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of ArrayList class
        // Declaring object of Integer type
        ArrayList<Integer> arr = new ArrayList<>();
 
        // Iterating over the object
        for (int i = 0; i < 100000; i++) {
 
            // Adding elements using add() method
            arr.add(i);
        }
 
        // Calculating and printing the time taken
        // to find 40000 using contains()
        long start = System.nanoTime();
        arr.contains(40000);
        long end = System.nanoTime();
 
        // Print statement
        System.out.println(
            "Time taken to find 40000 inside arr "
            + "using contains() = " + (end - start)
            + " nano seconds");
 
        // Calculating and printing the time taken
        // to find 40000 using Collections.binarySearch()
        start = System.nanoTime();
        Collections.binarySearch(arr, 40000);
        end = System.nanoTime();
 
        // Print statement
        System.out.println(
            "Time taken to find 40000 inside arr "
            + "using binarySearch() = " + (end - start)
            + " nano seconds");
    }
}


Output

Time taken to find 40000 inside arr using contains() = 6651276 nano seconds
Time taken to find 40000 inside arr using binarySearch() = 85231 nano seconds

Case 3: For an unsorted List

In the code mentioned below, we have created an unsorted ArrayList by storing random numbers between 0 and 100000 inside it. As the list is unsorted the performance of contains() method is better as it only takes O(n) time while for using the Collections.sort() method we first have to sort the list which takes an extra O(nlog(n)) time and then O(log2(n)) time is taken to search for the element.\

 

Example

Java




// Java program to compare the performance
// of contains() and Collections.sort() method
//  on an unsorted ArrayList (Case3)
 
// Importing ArrayList and Collections class
// from java.util package
import java.util.ArrayList;
import java.util.Collections;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of ArrayList class
        ArrayList<Integer> arr = new ArrayList<>();
 
        // Iterating between 0 to 100000 numbers
        for (int i = 0; i < 100000; i++) {
 
            // Generating random numbers as iterated
            // using random() function
            int rand = (int)(Math.random() * 100000);
 
            // Later storing them inside our list
            arr.add(rand);
        }
 
        // Setting the key to be found as the element
        // at index 30000 inside of unsorted list
        int key = arr.get(30000);
 
        // Calculating and printing the time taken
        // to find the key using contains()
        long start = System.nanoTime();
        arr.contains(key);
        long end = System.nanoTime();
 
        // Print statement
        System.out.println(
            "Time takes to find " + key
            + " inside arr using contains() = "
            + (end - start) + " nano seconds");
 
        // Calculating and printing the time taken to
        // find the key using Collections.binarySearch()
        // after sorting the list using Collections.sort()
        // method
        start = System.nanoTime();
        Collections.sort(arr);
        Collections.binarySearch(arr, key);
        end = System.nanoTime();
 
        // Print statement
        System.out.println(
            "Time takes to find " + key
            + " inside arr using binarySearch() = "
            + (end - start) + " nano seconds");
    }
}


Output

Time takes to find 66181 inside arr using contains() = 8331486 nano seconds
Time takes to find 66181 inside arr using binarySearch() = 140322701 nano seconds


Last Updated : 13 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads