Java Collections Class
In this page:
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
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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());
}
}
}
Login to try C/C++/Java/PHP code in the editor
- Calling
Collections.sort()on a list of objects that are notComparable, which does not compile or fails. - Modifying an unmodifiable list, which throws
UnsupportedOperationException. - Using
binarySearch()on an unsorted list, which gives wrong results.
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting