Collections.reverse rewrites the given list's elements in place rather than returning a new list, so it fails with UnsupportedOperationException on an immutable List.of.
List<Integer> nums = new ArrayList<>(List.of(1, 2, 3, 4, 5));
Collections.reverse(nums);
System.out.println("Reversed in place: " + nums);
List<Integer> immutable = List.of(1, 2, 3);
try {
Collections.reverse(immutable);
} catch (UnsupportedOperationException e) {
System.out.println("Reversing an immutable list throws: " + e.getClass().getSimpleName());
}
Reversed in place: [5, 4, 3, 2, 1]
Reversing an immutable list throws: UnsupportedOperationException
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-09-27