- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Collections.binarySearch() Method
Description
The Java binarySearch(List<? extends Comparable<? super T>>, T) method is used to search the specified list for the specified object using the binary search algorithm.
Declaration
Following is the declaration for java.util.Collections.binarySearch() method.
public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key)
Parameters
list − This is the list to be searched.
key − This is the key to be searched for.
Return Value
The method call returns the index of the search key, if it is contained in the list.
Exception
ClassCastException − This is thrown if the list contains elements that are not mutually comparable.
Java Collections.binarySearch(List<? extends T> list,T key,Comparator<? super T> c) Method
Description
The binarySearch(List<? extends T>, T, Comparator<? super T>) method is used to search the specified list for the specified object using the binary search algorithm. The list must be sorted into ascending order according to the specified comparator.
Declaration
Following is the declaration for java.util.Collections.binarySearch() method.
public static <T> int binarySearch(List<? extends T> list,T key,Comparator<? super T> c)
Parameters
list − This is the list to be searched.
key − This is the key to be searched for.
c − This is the comparator by which the list is ordered. A null value indicates that the elements' natural ordering should be used.
Return Value
The method call returns the index of the search key, if it is contained in the list.
Exception
ClassCastException − This is thrown if the list contains elements that are not mutually comparable using the specified comparator.
Performing Binary Search on a Collection of Strings Example
The following example shows the usage of Java Collection binarySearch(Collection,T) method to search an item in the specified collection. We've created a List object with some strings, printed the original list. Using binarySearch(Collection, T) method, we've searched a key from the list and then printed the search result.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<String> list = new ArrayList<>(Arrays.asList("Welcome","to","Tutorialspoint")); System.out.println("Collection: " + list); // search the list for item 'To' int index = Collections.binarySearch(list, "to"); System.out.println("'To' is available at index: "+index); } }
Output
Let us compile and run the above program, this will produce the following result −
Collection: [Welcome, to, Tutorialspoint] 'To' is available at index: 1
Performing Binary Search on a Collection of Comparable Objects Example
The following example shows the usage of Java Collection binarySearch(Collection,T) method to search an item in the specified collection. We've created a List object with some Student objects, printed the original list. Using binarySearch(Collection, T) method, we've searched a student from the list and then printed the search result.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Student> list = new ArrayList<>(Arrays.asList(new Student(1, "Julie"), new Student(2, "Robert"), new Student(3, "Adam"))); System.out.println("Collection: " + list); // search the list for student 'Julie' int index = Collections.binarySearch(list, new Student(1, "Julie")); System.out.println("Julie is available at index: "+index); } } class Student implements Comparable<Student> { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } @Override public boolean equals(Object obj) { Student s = (Student)obj; return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name); } @Override public int compareTo(Student student) { return this.rollNo - student.rollNo; } }
Output
Let us compile and run the above program, this will produce the following result −
Collection: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]] Julie is available at index: 0
Performing Binary Search on a Collection of Objects using Comparator Example
The following example shows the usage of Java Collection binarySearch(Collection,T, Comparator) method to search an item in the specified collection. We've created a List object with some Student objects, printed the original list. Using binarySearch(Collection, T, Comparator) method, we've searched a student from the list and then printed the search result.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Comparator; public class CollectionsDemo { public static void main(String[] args) { List<Student> list = new ArrayList<>(Arrays.asList(new Student(1, "Julie"), new Student(2, "Robert"), new Student(3, "Adam"))); System.out.println("Collection: " + list); RollNoComparator comparator = new RollNoComparator(); // search the list for student 'Julie' int index = Collections.binarySearch(list, new Student(1, "Julie"),comparator); System.out.println("Julie is available at index: "+index); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } @Override public boolean equals(Object obj) { Student s = (Student)obj; return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name); } public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class RollNoComparator implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { return o1.getRollNo()-o2.getRollNo(); } }
Output
Let us compile and run the above program, this will produce the following result −
Collection: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]] Julie is available at index: 0
To Continue Learning Please Login
Login with Google