Java / Java 21 Collection Framework features Interview questions
How do you apply reversed() to a List in Java 21?
Call .reversed() directly on any List instance - since List extends SequencedCollection, the method is available without any extra imports or wrapping.
List<Integer> original = new ArrayList<>(List.of(10, 20, 30)); List<Integer> reversedView = original.reversed(); System.out.println(reversedView); // [30, 20, 10]
The result is a live, reverse-ordered view backed by the original list, not a copy - iterating it, or calling getFirst() on it, walks the original list from the end backward.
If you need an independent snapshot instead of a view, wrap the result in a new list: new ArrayList<>(original.reversed()).
More Related questions...