java.util.Collections
is a utility class that provides static methods to operate on collections like List, Set, Map.
Methods
List<Integer> nums = Arrays.asList(3, 2, 1);
Collections.sort(nums); // [1, 2, 3]
Collections.shuffle(nums); // Random order
Collections.reverse(nums); // Reverses the list
Collections.swap(nums, 0, 2); // Swaps values at index 0 and 2
Collection.max(nums);
Collection.min(nums);
List<String> readOnly = Collections.unmodifiableList(myList);
readOnly.add("X"); // Throws UnsupportedOperationException
List<String> syncLIst = Collections.synchronizedList(new ArrayList<>());
Use Cases
- Sorting leaderboard scores.
- Shuffling quiz questions.
- Creating read-only collections (public APIs).
- Wrapping collections in thread-safe mode (legacy apps).
Tip
Be ready to:
- use sort(), reverse(), max() in coding rounds,
- explain the difference between Collections (utility) vs Collection (interface),
- mention unmodifiableList() when discussing immutability.
Parent: _Collections