Java Collections binarySearch() Method21 Mar 2025 | 4 min read The binarySearch() is an inbuilt method of Java Collections class which returns the position of the object in a sorted list. There are two different types of Java collections binarySearch() method which can be differentiated depending on its parameter. These are:
Java Collections binarySearch(List<? extends Comparable<? super T>> list, T key)This method is used to search the provided list for the specified object using the binary search algorithm. The list must be sorted into ascending order according to the specified natural number, prior to making the method call. If the list is not sorted, the results are undefined. Java Collections binarySearch(List<? extends T> list, T key, Comparator<? super T> c)This method is used to search the provided list for the specified object using the binary search algorithm. The list must be sorted into ascending order according to the specified comparator prior to making the method call. SyntaxFollowing is the declaration of binarySearch() method: Parameter
Returns
ExceptionsClassCastException- It throws exception if the elements of the list are not mutually comparable or the search key is not mutually comparable with the elements of the list. Compatibility VersionJava 1.5 and above Example 1Output: index 'D' is available at position: 3 Example 2Output: Provided List are: [10, -20, 30, -40, 50] Index '-20' is available at position: -4 Example 3Output: Provided List are: [10, -20, 30] Enter the search key: D Exception in thread "main" java.lang.ClassCastException: java.base/java.lang.Integer cannot be cast to java.base/java.lang.String at java.base/java.lang.String.compareTo(String.java:124) at java.base/java.util.Collections$ReverseComparator.compare(Collections.java:5140) at java.base/java.util.Collections$ReverseComparator.compare(Collections.java:5131) at java.base/java.util.Collections.indexedBinarySearch(Collections.java:333) at java.base/java.util.Collections.binarySearch(Collections.java:321) at myPackage.CollectionBinarySearchExample3.main(CollectionBinarySearchExample3.java:16) Example 4Output: 4 is available at index: 3 Example 5Output: Available at index: 0 Found at index: -1 Next TopicJava Collections Class |
Java Collections Method The method of Java Collections class is used to get an unmodifiable view of the specified sorted set. Syntax Following is the declaration of method: public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s) Parameter Parameter Description Required/Optional s It is the sorted set for which an unmodifiable view is to...
2 min read
Java Collections Method The is a Java Collections class method which returns the minimum value for the given inputs. There is two different types of Java method which can be differentiated depending on its parameter. These are: Java Collections min(coll) Method Java Collections min(coll, comp)...
3 min read
Java Collections Method The method of Java Collections class is used to get the starting position of the first occurrence of the specified target list within the specified source list. It returns -1 if there is no such occurrence in the specified list. Syntax Following is...
3 min read
Java Collections Method The method of Java Collections class copies all of the elements from one list into another list. In this method, the size of the destination list must be greater than or equal to the size of the source list. Syntax Following is the...
3 min read
Java Collections Method The method of Java Collections class is used to reverse the order of the elements in the specified list. Syntax Following is the declaration of method: public static void reverse(List<?> list) Parameter Parameter Description Required/Optional list It is the list whose elements are to be reversed. Required Returns The method does...
2 min read
Java Collections Method The method of Java Collections class is used to get a synchronized (thread-safe) sorted set backed by the specified sorted set. Syntax Following is the declaration of method: public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s) Parameter Parameter Description Required/Optional s It is the sorted set which will be wrapped in...
3 min read
Java Collections Method The method of Java Collections class is used to get a synchronized (thread-safe) collection backed by the specified list. Syntax Following is the declaration of method: public static <T> List<T> synchronizedList(List<T> list) Parameter Parameter Description Required/Optional list It is the list which will be wrapped in a synchronized list. Required Returns The...
2 min read
Java Collections Method The method of Java Collections class is used to rotate the elements in the specified list by a given distance. Syntax Following is the declaration of method: public static void rotate(List<?> list, int distance) Parameter Parameter Description Required/Optional list It is the list which will be rotated. Required distance It is...
3 min read
The sort() method of Java Collections class is used to sort the elements presents in the specified list. There is two different types of Java sort() method which can be differentiated depending on its parameter. These are: Java Collections sort(list) Method Java Collections sort(list, comp) Method Java Collections...
3 min read
Java Collections Method The is a Java Collections class method which works by randomly permuting the specified list elements. There is two different types of Java method which can be differentiated depending on its parameter. These are: Java Collections shuffle(list) Method Java Collections shuffle(list, random)...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India