Skip to main content

The Collections Class

The Java Collections Framework does not only provide data structures (interfaces and implementation classes). It also provides a highly powerful utility class named java.util.Collections.

[!WARNING] Do not confuse the Collection interface with the Collections utility class!

  • Collection: The root interface of the framework.
  • Collections: A utility class containing static methods to operate on collections.

Important Static Methods​

The Collections class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, "wrappers" (which return a new collection backed by a specified collection), and a few other odds and ends.

1. Collections.sort()​

Used to sort the elements present in the specified list of collections in ascending order.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class LabCollections1 {

public static void main(String args[]) {
List<String> list = new ArrayList<>();

list.add("Zebra");
list.add("Apple");
list.add("Mango");

System.out.println("Before sorting: " + list);

// Using the Collections utility class to sort the list
Collections.sort(list);

System.out.println("After sorting: " + list);
}
}

Output:

Before sorting: [Zebra, Apple, Mango]
After sorting: [Apple, Mango, Zebra]

2. Collections.reverse()​

Reverses the order of the elements in the specified list.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class LabCollections2 {

public static void main(String args[]) {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);

// Reverses the current order
Collections.reverse(list);

System.out.println("Reversed List: " + list); // [3, 2, 1]
}
}

3. Collections.binarySearch()​

Searches the specified list for the specified object using the binary search algorithm.

[!IMPORTANT] The list must be sorted in ascending order (using Collections.sort()) prior to making this call. If it is not sorted, the results are undefined.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class LabCollections3 {

public static void main(String args[]) {
List<Integer> list = new ArrayList<>();
list.add(50);
list.add(10);
list.add(30);

// 1. Sort the list first
Collections.sort(list); // List is now [10, 30, 50]

// 2. Perform Binary Search
int index = Collections.binarySearch(list, 30);

System.out.println("Element 30 found at index: " + index); // 1
}
}

4. Collections.max() and Collections.min()​

Returns the maximum or minimum element of the given collection, according to the natural ordering of its elements.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class LabCollections4 {

public static void main(String args[]) {
List<Integer> list = new ArrayList<>();
list.add(100);
list.add(5);
list.add(2000);

System.out.println("Maximum: " + Collections.max(list)); // 2000
System.out.println("Minimum: " + Collections.min(list)); // 5
}
}