Online Java Compiler By
JavaTpoint.com
import java.util.*; public class CollectionsBinarySearchExample3 { public static void main(String[] args) { //Create list ArrayList
arrlist = new ArrayList
(); arrlist.add(10); arrlist.add(-20); arrlist.add(30); //Print List System.out.println("Provided List are: "+arrlist); System.out.print("Enter the search key: "); Scanner sc = new Scanner (System.in); String key = sc.next(); int index = Collections.binarySearch(arrlist, key, Collections.reverseOrder()); System.out.println("Search Key is available at position: "+index); sc.close(); } }
Output