Java Collection Tutorial - Java Collections.binarySearch(List <? extends Comparable <? super T>> list, T key)








Syntax

Collections.binarySearch(List <? extends Comparable <? super T>> list, T key) has the following syntax.

public static <T> int binarySearch(List <? extends Comparable <? super T>> list,   T key)

Example

In the following code shows how to use Collections.binarySearch(List <? extends Comparable <? super T>> list, T key) method.

//from  w  w  w . j av a  2  s  .co m
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
   public static void main(String args[]) {
      
      List<String>  arlst=new ArrayList<String> ();
      
      arlst.add("CSS");
      arlst.add("HTML");
      arlst.add("PHP");
      
      
      // search the list for key 
      int index=Collections.binarySearch(arlst, "HTML");     
      
      System.out.println(index);
   }    
}

The code above generates the following result.