The Collections Utility Class in Java
Introduction
In modern applications, managing collections of objects often requires performing common algorithms—such as sorting a list of products by price, searching for a specific item, or reversing a list's order.
Instead of writing custom sorting and searching algorithms, Java provides a dedicated helper utility class: java.util.Collections.
This utility class consists entirely of static methods that operate on or return collections.
Collection vs. Collections: What's the Difference?
These two terms sound almost identical but represent entirely distinct parts of the language:
| Feature | Collection (Interface) | Collections (Class) |
|---|---|---|
| Type | Java Interface | Java Utility Class |
| Package | java.util.Collection | java.util.Collections |
| Purpose | Root interface for Lists, Sets, and Queues | Provides static utility helper methods |
| Instantiation | Cannot be instantiated directly | Cannot be instantiated (private constructor) |
| Example Method | add(), remove(), size() | sort(), reverse(), binarySearch() |
Essential Collections Utility Operations
1. Sorting a Collection (sort):
The Collections.sort() method sorts a List in natural ascending order. The elements in the list must implement the Comparable interface.
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> marks = new ArrayList<>();
marks.add(85);
marks.add(50);
marks.add(98);
marks.add(72);
System.out.println("Before Sorting: " + marks); // [85, 50, 98, 72]
Collections.sort(marks); // Sorts in ascending order
System.out.println("After Sorting: " + marks); // [50, 72, 85, 98]
}
}
2. Searching a Collection (binarySearch):
To perform a fast binary search on a list, the list must be sorted first. If the element is found, the method returns its index (0-based). If not found, it returns a negative insertion point.
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> marks = new ArrayList<>();
marks.add(50);
marks.add(72);
marks.add(85);
marks.add(98);
// Perform Binary Search
int index = Collections.binarySearch(marks, 85);
System.out.println("Index of 85: " + index); // Prints: 2
int notFound = Collections.binarySearch(marks, 60);
System.out.println("Result for 60: " + notFound); // Prints negative value
}
}
3. Reversing and Shuffling:
reverse(List<?> list): Reverses the order of elements in a list.shuffle(List<?> list): Randomly permutes the list elements (useful for game card shuffling or randomized tests).
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> cards = new ArrayList<>();
cards.add("Ace");
cards.add("King");
cards.add("Queen");
cards.add("Jack");
Collections.reverse(cards);
System.out.println("Reversed: " + cards); // [Jack, Queen, King, Ace]
Collections.shuffle(cards);
System.out.println("Shuffled: " + cards); // Randomized output
}
}
4. Extremums and Frequency Analysis:
max(Collection<?> coll): Returns the maximum element based on natural ordering.min(Collection<?> coll): Returns the minimum element based on natural ordering.frequency(Collection<?> c, Object o): Counts how many times objectoappears.
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(10);
list.add(30);
System.out.println("Max: " + Collections.max(list)); // 30
System.out.println("Min: " + Collections.min(list)); // 10
System.out.println("Frequency of 10: " + Collections.frequency(list, 10)); // 2
}
}
5. Swapping and Disjoint Check:
swap(List<?> list, int i, int j): Swaps the elements at indicesiandj.disjoint(Collection<?> c1, Collection<?> c2): Returnstrueif the two collections have no elements in common.
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> list1 = new ArrayList<>();
list1.add("A");
list1.add("B");
ArrayList<String> list2 = new ArrayList<>();
list2.add("C");
list2.add("D");
// Swap indices 0 and 1 in list1
Collections.swap(list1, 0, 1);
System.out.println("After Swap: " + list1); // [B, A]
// Check if lists are completely distinct
boolean distinct = Collections.disjoint(list1, list2);
System.out.println("Are disjoint? " + distinct); // true
}
}
Key Takeaways
Collectionsis a final utility class containing only static helper methods.Collectionis an interface;Collectionsis a utility class.- The
binarySearch()method requires the list to be sorted in ascending order beforehand. - The utility provides convenient, highly optimized algorithms for sorting, reversing, shuffling, swapping, and finding limits on lists.
Back to Module Home: Collection Framework Index