Collections.reverse() Method in Java with Examples
The reverse() method of the Collections class, as the name suggests, is used to reverse the order of elements in a list.
Note: It does not sort the elements, it simply reverses their current order.
This class is present in java.util package so the syntax is as follows:
import java.util.Collections;
Collections.reverse(class_obj);
Note: The reverse() method only reverses the order of elements in the list. It does not perform any sorting. If you need to sort the list, use the Collections.sort() method.
Illustration:
Input : {1, 2, 3, 4}
Output : {4, 3, 2, 1}
Parameter: Object of a class whose elements are to be reversed.
public static void reverse(List myList)
Exception Thrown: It throws UnsupportedOperationException if the specified list or its list-iterator does not support the set operation.
Let us see the usage of this method via use cases listed below as follows:
- Reversing an ArrayList
- Reversing a LinkedList
- Reversing an array
Let us implement this method of Collections class by implementing the same in clan java codes as provided below as follows:
Case 1: Reversing an ArrayList
// Java program to illustrate reverse() method
// of Collections class over ArrayList
// Importing utility classes
import java.util.*;
// Main class
public class GFG {
// main driver method
public static void main(String[] args)
{
// Let us create a list of strings
List<String> mylist = new ArrayList<String>();
// Adding elements to the List
// Custom input elements
mylist.add("practice");
mylist.add("code");
mylist.add("quiz");
mylist.add("geeksforgeeks");
// Print all elements originally
System.out.println("Original List : " + mylist);
// Using reverse() method to
// reverse the element order of mylist
Collections.reverse(mylist);
// Print all elements of list in reverse order
// using reverse() method
System.out.println("Modified List: " + mylist);
}
}
Output
Original List : [practice, code, quiz, geeksforgeeks] Modified List: [geeksforgeeks, quiz, code, practice]
Case 2: Reversing a LinkedList
// Java program to illustrate reverse() method
// of Collections class over LinkedList
// Importing utility classes
import java.util.*;
// Main class
public class GFG {
// main driver method
public static void main(String[] args)
{
// Let us create a list of strings
List<String> mylist = new LinkedList<String>();
// Adding elements to the List
// Custom input elements
mylist.add("practice");
mylist.add("code");
mylist.add("quiz");
mylist.add("geeksforgeeks");
// Print all elements originally
System.out.println("Original List : " + mylist);
// Using reverse() method to
// reverse the element order of mylist
Collections.reverse(mylist);
// Print all elements of list in reverse order
// using reverse() method
System.out.println("Modified List: " + mylist);
}
}
Output
Original List : [practice, code, quiz, geeksforgeeks] Modified List: [geeksforgeeks, quiz, code, practice]
If we peek through the above programs then there is only a minute signature detail that we are just creating an object of LinkedList class instead of Array class as seen in example1A. For LinkedList, we just did change as shown below in the above codes:
LinkedList in "List mylist = new ArrayList();".
Case 3: Reversing an array: Arrays class in Java doesn't have a reverse method. We can use Collections.reverse() to reverse an array also as shown below as follows:
Example
// Java program to Illustrate Reversal of Array
// using reverse() method of Collections class
// Importing utility classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating input integer array
Integer arr[] = { 10, 20, 30, 40, 50 };
// Print elements of array
System.out.println("Original Array : "
+ Arrays.toString(arr));
// Reversing elements of array using reverse()
// method of Collections class and fetching as
// list via asList()
Collections.reverse(Arrays.asList(arr));
// Print and display reverse updated array
System.out.println("Modified Array : "
+ Arrays.toString(arr));
}
}
Output
Original Array : [10, 20, 30, 40, 50] Modified Array : [50, 40, 30, 20, 10]