Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

Collections.reverse()

Reverses the order of list elements in place.

Available since: Java 1.2

Syntax

static void reverse(List<?> list)

Parameters

ParameterTypeDescription
listList<?>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

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.

Thanks for reading!

Donate to the site — we work hard for you.