The Collections.reverse() method in Java is used to reverse the order of elements in a List. It belongs to the java.util.Collections class and modifies the existing list directly.
- It changes the position of every element so that the last element becomes the first and the first becomes the last.
- The reversal is performed directly on the given list, so no separate reversed list is created.
- It supports list implementations such as ArrayList and LinkedList.
Working of Collections.reverse()
The reverse() method rearranges the elements of the given list by swapping elements from opposite ends.
Original: [10, 20, 30, 40, 50]
Reversed: [50, 40, 30, 20, 10]
Syntax:
Collections.reverse(List<?> list)
Example: Reverse an ArrayList
import java.util.*;
class ReverseArrayList {
public static void main(String[] args)
{
List<Integer> numbers = new ArrayList<>();
numbers.add(15);
numbers.add(25);
numbers.add(35);
numbers.add(45);
numbers.add(55);
System.out.println("Original List: " + numbers);
// Reverse the list
Collections.reverse(numbers);
// Display the reversed list
System.out.println("Reversed List: " + numbers);
}
}
Output
Original List: [15, 25, 35, 45, 55] Reversed List: [55, 45, 35, 25, 15]
Explanation: The numbers list initially contains the elements in ascending order. The Collections.reverse(numbers) method reverses the order of these elements and updates the same list.
Example: Reverse a LinkedList
import java.util.*;
class ReverseLinkedList {
public static void main(String[] args) {
// Create a LinkedList
List<String> cities = new LinkedList<>();
// Add elements
cities.add("Delhi");
cities.add("Mumbai");
cities.add("Chennai");
cities.add("Jaipur");
// Display the original list
System.out.println("Original List: " + cities);
// Reverse the list
Collections.reverse(cities);
// Display the reversed list
System.out.println("Reversed List: " + cities);
}
}
Output
Original List: [Delhi, Mumbai, Chennai, Jaipur] Reversed List: [Jaipur, Chennai, Mumbai, Delhi]
Explanation: The cities list is created using LinkedList. Calling Collections.reverse(cities) changes the order of the elements from the last element to the first.
Example: Reverse an Array Using Collections.reverse()
import java.util.*;
class ReverseArray {
public static void main(String[] args)
{
// Create an Integer array
Integer[] numbers = { 12, 24, 36, 48, 60 };
// Display the original array
System.out.println("Original Array: " + Arrays.toString(numbers));
// Convert array to list and reverse it
Collections.reverse(Arrays.asList(numbers));
// Display the reversed array
System.out.println("Reversed Array: " + Arrays.toString(numbers));
}
}
Output
Original Array: [12, 24, 36, 48, 60] Reversed Array: [60, 48, 36, 24, 12]
Explanation:Arrays.asList(numbers) provides a list view backed by the original Integer array. The Collections.reverse() method reverses this list view, which also changes the order of elements in the original array.
Note: This approach works with wrapper-type arrays such as Integer[]. It does not work directly with primitive arrays such as int[].