//There are many methods provided in the Collections class and we’ll cover a subset of those below:
//binarySearch(): Performs binary search over a List to find the specified object and returns the index if found. This method is overloaded to also accept a Comparator in order to define a custom sorting algorithm.
//max(): Finds and returns the maximum element in the Collection. This method is overloaded to also accept a Comparator in order to define a custom sorting algorithm.
//min(): Finds and returns the minimum element in the Collection. This method is overloaded to also accept a Comparator in order to define a custom sorting algorithm.
//reverse(): Reverses the order of elements in the List passed in as an argument.
//sort(): Sorts the List passed in as an argument. This method is overloaded to also accept a Comparator in order to define a custom sorting algorithm.
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Collection;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
List<Integer> myList = new ArrayList<>();
myList.add(3);
myList.add(-1);
myList.add(57);
myList.add(29);
Set<String> mySet = new HashSet<>();
mySet.add("Hello");
mySet.add("World");
System.out.println("mySet max: \""+ Collections.max(mySet)+"\"");
System.out.println();
System.out.println("myList min: "+ Collections.min(myList));
System.out.println();
System.out.println("Index of 57 in myList is: "+Collections.binarySearch(myList, 57));
System.out.println();
System.out.println("myList prior to reverse: ");
printCollection(myList);
System.out.println();
Collections.reverse(myList);
System.out.println("myList reversed: ");
printCollection(myList);
System.out.println();
System.out.println("myList prior to sort: ");
printCollection(myList);
System.out.println();
Collections.sort(myList);
System.out.println("myList after sort: ");
printCollection(myList);
}
public static void printCollection(Collection<?> collection){
Iterator<?> myItr = collection.iterator();
while(myItr.hasNext()){
System.out.println(myItr.next());
}
}
}