How to search user defined object from a List by using binary search using comparator?

The java.util.Collections class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, “wrappers”, which return a new collection backed by a specified collection, and a few other odds and ends.

Collections.binarySearch() method will searches the specified list for the specified object using the binary search algorithm.

Example

package com.w3spoint;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; 
 
public class Test {
    public static void main(String args[]){
                List list = new ArrayList();
                list.add("ajay");
                list.add("amit");
                list.add("vikas");
                list.add("anil");
                int index = Collections.binarySearch(list,"vikas");
                System.out.println("Index : "+index);
    }
}

Output

Index : 2

Java Collections class examples

Please follow and like us:
Content Protection by DMCA.com