Open In App

Collections.reverse() Method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

reverse() method of Collections class as the name itself suggests is used for reversing elements been there up in the object in which they are stored. It reverses the order of elements in a list passed as an argument.

This class is present in java.util package so do syntax is as follows:

import java.util.Collections;
Collections.reverse(class_obj);

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




// 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




// 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 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




// 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 Arrays 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]


Last Updated : 10 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads