Last Updated on 11 April 2024   |   Print Email
In this Java tutorial, you will learn how to use the Collections utility class in the Java Collections framework to search for elements in a collection.You know, the java.util.Collections class provides reusable functionalities that operation on collections such as finding extremes values in a collection and searching for specific values in a list. These are grouped into “generic algorithms” category. In this article, we help you understand how to use these functionalities with code examples.
The methods in this group allow us to find the maximum and minimum elements in a collection, in terms of natural ordering or using a specified comparator.
The following method returns the maximum element in a given collection, according to the natural ordering of its elements, which must implement the Comparable interface:
public static T max(Collection<? extends T> collection)
The following method returns the minimum element in a given collection, according to the natural ordering of its elements, which must implement the Comparable interface:
public static T min(Collection<? extends T> collection)
The following method searches the specified list for the specified object using the binary search algorithm:
public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key)
Note that the list must be sorted into ascending order according to the natural ordering of its elements before invoking this method. Otherwise the results are undefined.This method returns the index of the search key if it is contained in the list (in case the list contains multiple elements equal to the specified key, there is no guarantee which one will be found). Also note that the return value always >=0 if and only if the key is found.The following code example searches for an employee whose name is “Jim” in the list of employees above:
Employee jim = new Employee("Jim");
Collections.sort(listEmployees);
int index = Collections.binarySearch(listEmployees, jim);
if (index >= 0) {
jim = listEmployees.get(index);
System.out.println("Found employee: " + jim);
}
Output:
Found employee: Jim (salary: 70000)
Binary search using a comparator:
The Collectionsutility class also provides an overloaded method that searches the specified list for the specified object according to the order determined by the specified comparator:
public static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c)
Note that the list must be sorted into ascending order according to the specified comparator. Otherwise the results are undefined.For example, the following code searches for an employees who salary is 70,000:
Comparator<Employee> comparator = new Comparator<Employee>() {
public int compare(Employee emp1, Employee emp2) {
return emp1.salary - emp2.salary;
}
};
Collections.sort(listEmployees, comparator);
Employee keyEmp = new Employee(70000);
int index = Collections.binarySearch(listEmployees, keyEmp, comparator);
if (index >= 0) {
keyEmp = listEmployees.get(index);
System.out.println("Found: " + keyEmp);
}
The following method returns the starting position of the last occurrence of the specified target list within the specified source list (or return -1 if the is no such occurrence):
public static int lastIndexOfSubList(List<?> source, List<?> target)
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments
Digital marketing course in Hyderabad