Java Program to Search User Defined Object From a List By Using Binary Search Using Comparator
The Comparator interface in Java can be used to compare user-defined objects. The Comparator interface is present in java.util package. Binary search is a searching algorithm that uses the divide and conquers rule to search the presence of an element in a list or array. The binary search algorithm works only on a sorted list. In case the list is not sorted the search may return undefined results. The predefined classes that implement the Collection interfaces also implement the comparable interface. Hence, when a binary search is performed on the objects of the predefined wrapper classes the objects are sorted in a natural order and then the binary search is performed on the sorted list. By default, user-defined classes do not implement the Comparable interface in Java. For user-defined classes, the Comparator interface is of great use. The Comparator interface allows the comparison of specific attributes of the user-defined class. The Comparator interface can also be used to compare two different objects belonging to different classes. Classes that implement the Comparator interface override the compare() method of the interface. The following examples deal with creating user-defined objects and using the Comparator interface to sort the objects and finally perform a binary search to get the index of the required object.
public static int binarySearch(List list, T key)
- list: Object list on which binary search is to be performed.
- key: The object to be found
Return Value: Returns the index of the key element. If the key element is not found the index returned is (-(insertion point) – 1).
A student class is created with attributes student id, student name, and student marks. The Student class object list is not in sorted order. The student object list is sorted using the comparator object passed to Collections.sort() method. The binary search is performed on the sorted Student list using Collections,binarySearch() method. If the target object is found the respective index is returned else the index returned is -(insertion point) – 1. The insertion point is the index where the target element must be placed on the list if it is not present. The student id is unique for every student hence it is used to sort the Student list. The compare() method is overridden in the StudentComp class. The compare() method returns 0 if two students have the same student id, returns +1 if the student id of s1 is greater than s2, and returns -1 otherwise. The Student list is sorted in ascending order. Finally, binary search is called on the sorted and the index required object is printed to the console.
Index of the searched key: 6 Index of the searched key: -1
In this example, the Student list is sorted in descending order of the marks attribute of the Student class. Therefore, the same objects of the previous example now have a different index. The binary search is performed on the new list and thus the index of the required element is returned and printed to the output.
0 Liam 100 1 Jane 98 2 Archie 96 3 Jack 95 4 Mary 90 5 Nate 89 6 Betty 85 7 Beck 75 Index of the searched key: 0 Index of the searched key: -9