Collections.reverse()
Reverses the order of list elements in place.
Available since: Java 1.2
Syntax
static void reverse(List<?> list)Parameters
| Parameter | Type | Description |
|---|---|---|
| list | List<?> | The modifiable list whose order is reversed. |
Examples
Reversing a list:
void main() {
List<Integer> nums = new ArrayList<>(List.of(1, 2, 3, 4));
Collections.reverse(nums);
IO.println(nums);
}Output:
[4, 3, 2, 1]Notes
The method only reverses the current order; it does not sort descending. For descending order use list.sort(Comparator.reverseOrder()). If the original list must not change, take the view list.reversed() (Java 21).
See also
- List.reversed() — Returns the list in reverse order without copying it.
- Collections.sort() — Sorts a list in place.
- Collections.shuffle() — Shuffles list elements into a random order.
Related recipes
In the tutorial
- Collection helper methods — The Collections class and useful Java methods: max, min, frequency, reverse, shuffle, nCopies, unmodifiable copies and views, Arrays.asList.