How to Reverse ArrayList in Java with Example

To reverse an ArrayList in java, one can use Collections class reverse method i.e Collections.reverse() method. Collections reverse method reverses the element of ArrayList in linear time i.e time complexity is O(n). Collections reverse method accepts a List type as an argument. So any List type  can be reversed  i.e LinkedList, Vector or any custom implementation. Lets write the java program to reverse an ArrayList in java.

Read Also :   Difference between ArrayList and LinkedList in Java


Java Program to Reverse ArrayList in Java

Below is the java program to reverse ArrayList in java. In the program we have added tech companies name in ascending order, after calling reverse method the names will be printed in descending order.



import java.util.ArrayList;
import java.util.Collections;
 
public class ReverseArrayListExample
{
    public static void main(String[] args) 
    {
        //Constructing an ArrayList
         
        ArrayList<String> list = new ArrayList<String>();
                 
        list.add("Apple");
         
        list.add("Amazon");
         
        list.add("Facebook");
         
        list.add("Google");
         
        list.add("IBM");
         
        list.add("Tesla");
                 
        //Printing list before reverse
         
        System.out.println("ArrayList Before Reverse :");
         
        System.out.println(list);
         
        //Reversing the list using Collections.reverse() method
         
        Collections.reverse(list);
         
        //Printing list after reverse
         
        System.out.println("ArrayList After Reverse :");
         
        System.out.println(list);
    }
}
Output :

ArrayList Before Reverse :
[Apple, Amazon, Facebook, Google, IBM, Tesla]
ArrayList After Reverse :
[Tesla, IBM, Google, Facebook, Amazon, Apple]


Java Program to Reverse LinkedList in Java



import java.util.Collections;
import java.util.LinkedList;
 
public class ReverseLinkedListExample
{
    public static void main(String[] args) 
    {
        //Constructing a LinkedList
         
        LinkedList<Integer> list = new LinkedList<Integer>();
         
        list.add(23);
         
        list.add(42);
         
        list.add(56);
         
        list.add(75);
         
        list.add(78);
         
        list.add(80);
         
        list.add(96);
         
        //Printing list before reverse
         
        System.out.println("LinkedList Before Reverse :");
         
        System.out.println(list);
         
        //Reversing the list using Collections.reverse() method
         
        Collections.reverse(list);
         
        //Printing list after reverse
         
        System.out.println("LinkedList After Reverse :");
         
        System.out.println(list);
    }
}
Output :

LinkedList Before Reverse :
[23, 42, 56, 75, 78, 80, 96]
LinkedList After Reverse :
[96, 80, 78, 75, 56, 42, 23]

Please mention in comments if you know any other way to reverse the ArrayList in Java or reverse the LinkedList in java.



About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry