The java.util.Collections class consists exclusively of static utility methods that operate on or return collections. It provides algorithms for sorting, reversing, searching, shuffling, and creating thread-safe or immutable views.
java.util.Collections Methodsimport java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionsUtilityDemo {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
numbers.add(45);
numbers.add(12);
numbers.add(89);
numbers.add(33);
// 1. sort() - In-place TimSort:
Collections.sort(numbers);
System.out.println("Sorted: " + numbers); // [12, 33, 45, 89]
// 2. reverse() - Inverts element order:
Collections.reverse(numbers);
System.out.println("Reversed: " + numbers); // [89, 45, 33, 12]
// 3. min() and max() algorithms:
System.out.println("Min Value: " + Collections.min(numbers)); // 12
System.out.println("Max Value: " + Collections.max(numbers)); // 89
// 4. shuffle() - Randomizes order using Fisher-Yates:
Collections.shuffle(numbers);
System.out.println("Shuffled: " + numbers);
// 5. unmodifiableList() - Creates read-only wrapper:
List<Integer> readOnly = Collections.unmodifiableList(numbers);
// readOnly.add(99); // Throws UnsupportedOperationException!
}
}
Congratulations on mastering the Java Collections Framework! Let us now step into Section 6: Generics, Lambdas, Functional Programming & Concurrency. Proceed to: Java Generics & Wildcards →
Accelerate your Java mastery by studying these tightly connected core tutorials:
Root framework overview.
List manipulation.
Custom sorting arguments.
Entering Section 6: Generics & Streams.