Java Collections reverse() Method
Example
Reverse the order of a list:
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
Collections.reverse(cars);
System.out.println(cars);
}
}
Definition and Usage
The reverse() method reverses the order of the items in a list. The last item becomes the first item, and the first item becomes the last item.
It does not sort the list. To sort a list in descending order, use sort() with reverseOrder().
Syntax
public static void reverse(List list)
Parameter Values
| Parameter | Description |
|---|---|
| list | Required. The list to reverse. |
Technical Details
| Returns: | No return value. |
|---|
Related Pages
Java Collections Framework Tutorial
❮ Collections Methods