How can you reverse the elements of a list?
It's a little more tricky than what it seems.
Question
How can you reverse the elements of a list?
Less Short Answer
There is a factory method in the Collections factory class simply called reverse that does exactly that.
javaList<String> names = new ArrayList<>(List.of("Ana", "Bob", "Cid"));
Collections.reverse(names);
// names = ["Cid", "Bob", "Ana"]Note that there are several traps in this method.
Trap One: A List, Not a Collection
This method takes a List and not a Collection, because a Collection is not ordered, so it does not make sense to try to reverse it.
Trap Two: It Modifies Your List
This method modifies your list, so your list needs to be modifiable, and it also needs to have a ListIterator that supports the set operation. That's the case for an ArrayList, a LinkedList, and even the list you get when you call Arrays.asList(...) — but this is still something you need to keep in mind if your application uses other implementations.
javaList<Integer> fixedSize = Arrays.asList(1, 2, 3);
Collections.reverse(fixedSize); // OK: fixedSize = [3, 2, 1]
List<Integer> immutable = List.of(1, 2, 3);
Collections.reverse(immutable); // throws UnsupportedOperationExceptionA View Instead: `reversed()`
You also have a reversed() method, added as part of the SequencedCollection interface extended by List. This reversed() method returns a view on your original list, without modifying it — so it also works on non-modifiable lists.
javaList<Integer> immutable = List.of(1, 2, 3);
List<Integer> view = immutable.reversed();
// view = [3, 2, 1], immutable is untouchedNote that this view is modifiable if the original list is itself modifiable, and in that case, modifying the view modifies the original list.
javaList<Integer> original = new ArrayList<>(List.of(1, 2, 3));
List<Integer> view = original.reversed();
view.set(0, 99);
// original = [1, 2, 99]One Last Word
Using Collections.reverse may be costly, as it needs to process your whole list to reverse it. Creating a view with reversed() is a lightweight process, because what you get is a wrapper that operates lazily on your list.