← Back to Core Java Course | Chapter 11: Collections | Lesson 16 of 17

Java Collections Class

The Collections class is a toolbox of helpers for collections, like sorting, reversing, shuffling, and searching. It saves you writing those steps yourself.
Syntax
java
import java.util.Collections;

Collections.sort(list);
Collections.reverse(list);
Collections.max(collection);

Sorting Collections

Collections.sort() reorders a List in place according to natural ordering or a supplied Comparator, saving you from implementing a sorting algorithm yourself for everyday use cases.

Example: Sorting Collections

java
// Import java.util.Collections so it can be used by its short name
import java.util.Collections;
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new ArrayList object and store it in nums
		ArrayList<Integer> nums = new ArrayList<>(java.util.List.of(3, 1, 2));
		Collections.sort(nums);
		// Print a line to the console
		System.out.println(nums);
	}
}

Reversing and Shuffling

Collections.reverse() flips a list's order in place, and Collections.shuffle() randomizes it — both operate directly on the list you pass in rather than returning a new one.

Example: Reversing and Shuffling

java
// Import java.util.Collections so it can be used by its short name
import java.util.Collections;
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new ArrayList object and store it in nums
		ArrayList<Integer> nums = new ArrayList<>(java.util.List.of(1, 2, 3));
		Collections.reverse(nums);
		// Print a line to the console
		System.out.println(nums);
	}
}

Searching Collections

Collections.binarySearch() finds an element's index in a *sorted* list in O(log n) time — calling it on an unsorted list gives undefined, unreliable results, so always sort first.

Example: Searching Collections

java
import java.util.Collections;
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		ArrayList<Integer> nums = new ArrayList<>(java.util.List.of(1, 3, 5, 7));
		int index = Collections.binarySearch(nums, 5); // list must already be sorted
		System.out.println(index);
	}
}

Modifying Lists

Methods like Collections.max(), min(), frequency(), and swap() let you query or rearrange list contents without writing manual loops for these very common operations.

Example: Modifying Lists

java
// Import java.util.Collections so it can be used by its short name
import java.util.Collections;
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new ArrayList object and store it in nums
		ArrayList<Integer> nums = new ArrayList<>(java.util.List.of(3, 1, 4, 1));
		// Print a line to the console
		System.out.println(Collections.max(nums));
		// Print a line to the console
		System.out.println(Collections.frequency(nums, 1));
		Collections.swap(nums, 0, 1);
		System.out.println(nums);
	}
}

Unmodifiable Collections

Collections.unmodifiableList() (and its Set/Map equivalents) wraps a collection so any attempted modification throws UnsupportedOperationException, letting you safely hand out a read-only view of internal data.

Example: Unmodifiable Collections

java
// Import java.util.Collections so it can be used by its short name
import java.util.Collections;
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
import java.util.List;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		List<Integer> readOnly = Collections.unmodifiableList(new ArrayList<>(java.util.List.of(1, 2)));
		// Try code that might throw an exception
		try {
			readOnly.add(3);
		// Handle UnsupportedOperationException if it was thrown
		} catch (UnsupportedOperationException e) {
			// Print a line to the console
			System.out.println("Cannot modify: " + e.getClass().getSimpleName());
		}
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Calling Collections.sort() on a list of objects that are not Comparable, which does not compile or fails.
  2. Modifying an unmodifiable list, which throws UnsupportedOperationException.
  3. Using binarySearch() on an unsorted list, which gives wrong results.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.