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.*;
 
public class ReverseArrayList
{
    public static void main(String[] args) 
    {
        //Creating an ArrayList object         
        ArrayList<String> arrlist = new ArrayList<String>(); 
 
        //Adding elements to ArrayList object 
        arrlist.add("Apple");         
        arrlist.add("Amazon");        
        arrlist.add("Facebook");
        arrlist.add("Google");
        arrlist.add("IBM");         
        arrlist.add("Tesla");
                 
        //Displaying ArrayList Before Reverse         
        System.out.println("Before Reverse ArrayList:");         
        System.out.println(arrlist);
         
        /*Reversing the list using 
          Collections.reverse() method*/         
        Collections.reverse(arrlist);
         
        //Displaying list after reverse         
        System.out.println("After Reverse ArrayList:");         
        System.out.println(arrlist);
    }
} 

Output :

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


Java Program to Reverse LinkedList in Java


import java.util.*;
 
public class ReverseLinkedList
{
    public static void main(String[] args) 
    {
        //Creating a LinkedList object          
        LinkedList<Integer> linkedlist = new LinkedList<Integer>(); 
 
        //Adding elements to LinkedList object 
        linkedlist.add(23);        
        linkedlist.add(42);
        linkedlist.add(56);
        linkedlist.add(75);
        linkedlist.add(78);         
        linkedlist.add(80);
        linkedlist.add(96);
         
        //Displaying linkedlist before reverse         
        System.out.println("Before Reverse LinkedList:");         
        System.out.println(linkedlist);
         
        /*Reversing the list using 
          Collections.reverse() method*/        
        Collections.reverse(linkedlist);
         
        //Displaying linkedlist after reverse         
        System.out.println("After Reverse LinkedList:");         
        System.out.println(linkedlist);
    }
}

Output :

Before Reverse LinkedList:
[23, 42, 56, 75, 78, 80, 96]
After Reverse LinkedList:
[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.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry