Java / Java 21 Collection Framework features Interview questions
What is the difference between List.reversed() and Collections.reverse()?
list.reversed(), new in Java 21, returns a fresh reverse-ordered view of the list and leaves the original list's own order completely untouched.
Collections.reverse(list), which has existed since Java 1.2, mutates the list in place, physically swapping elements so the original list itself ends up reordered.
List<Integer> a = new ArrayList<>(List.of(1, 2, 3)); List<Integer> viewed = a.reversed(); // a is still [1, 2, 3] List<Integer> b = new ArrayList<>(List.of(1, 2, 3)); Collections.reverse(b); // b is now [3, 2, 1]
Use reversed() when you just want to read or iterate in the opposite direction without disturbing the original list; use Collections.reverse() when you actually need the original list's order permanently changed.
More Related questions...