java.util.Collections.reverse() Method

Advertisements


Description

The reverse(List<?>) method is used to reverse the order of the elements in the specified list.

Declaration

Following is the declaration for java.util.Collections.reverse() method.

public static void reverse(List<?> list)			 

Parameters

  • list--This is the list whose elements are to be reversed.

Return Value

NA

Exception

  • UnsupportedOperationException--This is if the specified list or its list-iterator does not support the set operation.

Example

The following example shows the usage of java.util.Collections.reverse()

package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String[] args) {
      // create array list object
      ArrayList arrlst = new ArrayList();
      
      // populate the list
      arrlst.add("A");
      arrlst.add("B");
      arrlst.add("C");
      arrlst.add("D");
      arrlst.add("E");

      System.out.println("The initial list is :"+arrlst);
      
      // reverse the list
      Collections.reverse(arrlst);
      
      System.out.println("The Reverse List is :"+arrlst);
   }
}

Let us compile and run the above program, this will produce the following result.

The initial list is :[A, B, C, D, E]
The Reverse List is :[E, D, C, B, A]



Advertisements
Advertisements