Reverse an ArrayList in Java

13 May, 2021
Assuming you have gone through arraylist in java and know about arraylist. This post contains different examples for reversing an arraylist which are given below:1. By writing our own function(Using additional space): reverseArrayList() method in RevArrayList class contains logic for reversing an arraylist with integer objects. This method takes an arraylist as a parameter, traverses in reverse order and adds all the elements to the newly created arraylist. Finally the reversed arraylist is returned. 
Java
// Java program for reversing an arraylistimport java.io.*;import java.util.*;class RevArrayList {     // Takes an arraylist as a parameter and returns    // a reversed arraylist    public ArrayList<Integer> reverseArrayList(ArrayList<Integer> alist)    {        // Arraylist for storing reversed elements        ArrayList<Integer> revArrayList = new ArrayList<Integer>();        for (int i = alist.size() - 1; i >= 0; i--) {             // Append the elements in reverse order            revArrayList.add(alist.get(i));        }         // Return the reversed arraylist        return revArrayList;    }     // Iterate through all the elements and print    public void printElements(ArrayList<Integer> alist)    {        for (int i = 0; i < alist.size(); i++) {            System.out.print(alist.get(i) + " ");        }    }} public class GFG {    public static void main(String[] args)    {        RevArrayList obj = new RevArrayList();         // Declaring arraylist without any initial size        ArrayList<Integer> arrayli = new ArrayList<Integer>();         // Appending elements at the end of the list        arrayli.add(new Integer(1));        arrayli.add(new Integer(2));        arrayli.add(new Integer(3));        arrayli.add(new Integer(4));        System.out.print("Elements before reversing:");        obj.printElements(arrayli);        arrayli = obj.reverseArrayList(arrayli);        System.out.print("\nElements after reversing:");        obj.printElements(arrayli);    }}
Elements before reversing:1 2 3 4 
Elements after reversing:4 3 2 1
 
1. By writing our own function(without using additional space): In the previous example, an arraylist is used additionally for storing all the reversed elements which takes more space. To avoid that, same arraylist can be used for reversing. Logic: 1. Run the loop for n/2 times where ‘n’ is the number of elements in the arraylist. 2. In the first pass, Swap the first and nth element 3. In the second pass, Swap the second and (n-1)th element and so on till you reach the mid of the arraylist. 4. Return the arraylist after the loop termination. 
Java
// Java program for reversing an arraylistimport java.io.*;import java.util.*;class RevArrayList {     // Takes an arraylist as a parameter and returns    // a reversed arraylist    public ArrayList<Integer> reverseArrayList(ArrayList<Integer> alist)    {        // Arraylist for storing reversed elements        // this.revArrayList = alist;        for (int i = 0; i < alist.size() / 2; i++) {            Integer temp = alist.get(i);            alist.set(i, alist.get(alist.size() - i - 1));            alist.set(alist.size() - i - 1, temp);        }         // Return the reversed arraylist        return alist;    }     // Iterate through all the elements and print    public void printElements(ArrayList<Integer> alist)    {        for (int i = 0; i < alist.size(); i++) {            System.out.print(alist.get(i) + " ");        }    }} public class GFG1 {    public static void main(String[] args)    {        RevArrayList obj = new RevArrayList();         // Declaring arraylist without any initial size        ArrayList<Integer> arrayli = new ArrayList<Integer>();         // Appending elements at the end of the list        arrayli.add(new Integer(12));        arrayli.add(new Integer(13));        arrayli.add(new Integer(123));        arrayli.add(new Integer(54));        arrayli.add(new Integer(1));        System.out.print("Elements before reversing: ");        obj.printElements(arrayli);        arrayli = obj.reverseArrayList(arrayli);        System.out.print("\nElements after reversing: ");        obj.printElements(arrayli);    }}
Elements before reversing: 12 13 123 54 1 
Elements after reversing: 1 54 123 13 12
 
2. By using Collections class: Collections is a class in java.util package which contains various static methods for searching, sorting, reversing, finding max, min....etc. We can make use of the In-built Collections.reverse() method for reversing an arraylist. It takes a list as an input parameter and returns the reversed list. 
Java
// Java program for reversing an arraylistimport java.io.*;import java.util.*; public class GFG2 {    public static void main(String[] args)    {        // Declaring arraylist without any initial size        ArrayList<Integer> arrayli = new ArrayList<Integer>();         // Appending elements at the end of the list        arrayli.add(new Integer(9));        arrayli.add(new Integer(145));        arrayli.add(new Integer(878));        arrayli.add(new Integer(343));        arrayli.add(new Integer(5));        System.out.print("Elements before reversing: ");        printElements(arrayli);         // Collections.reverse method takes a list as a        // parameter and reverses the passed parameter      //(no new array list is required)        Collections.reverse(arrayli);        System.out.print("\nElements after reversing: ");        printElements(arrayli);    }     // Iterate through all the elements and print    public static void printElements(ArrayList<Integer> alist)    {        for (int i = 0; i < alist.size(); i++) {            System.out.print(alist.get(i) + " ");        }    }}
Elements before reversing: 9 145 878 343 5 
Elements after reversing: 5 343 878 145 9
 
3. Reversing an arraylist of user defined objects: An Employee class is created for creating user defined objects with employeeID, employeeName, departmentName as class variables which are initialized in the constructor. An arraylist is created that takes only Employee(user defined) Objects. These objects are added to the arraylist using add() method. The arraylist is reversed using In-built reverse() method of Collections class. The printElements() static method is used only to avoid writing one more class in the program. 
Java
// Java program for reversing an arraylistimport java.io.*;import java.util.*;class Employee {    int empID;    String empName;    String deptName;     // Constructor for initializing the class variables    public Employee(int empID, String empName, String deptName)    {        this.empID = empID;        this.empName = empName;        this.deptName = deptName;    }} public class GFG3 {    public static void main(String[] args)    {        // Declaring arraylist without any initial size        ArrayList<Employee> arrayli = new ArrayList<Employee>();         // Creating user defined objects        Employee emp1 = new Employee(123, "Rama", "Facilities");        Employee emp2 = new Employee(124, "Lakshman", "Transport");        Employee emp3 = new Employee(125, "Ravan", "Packing");         // Appending all the objects for arraylist        arrayli.add(emp1);        arrayli.add(emp2);        arrayli.add(emp3);         System.out.print("Elements before reversing: ");        printElements(arrayli);         // Collections.reverse method takes a list as a        // parameter and reverse the list        Collections.reverse(arrayli);        System.out.print("\nElements after reversing: ");        printElements(arrayli);    }     // Iterate through all the elements and print    public static void printElements(ArrayList<Employee> alist)    {        for (int i = 0; i < alist.size(); i++) {            System.out.print("\n EmpID:" + alist.get(i).empID +             ", EmpName:" + alist.get(i).empName + ", Department:" +                                          alist.get(i).deptName);        }    }}
Elements before reversing: 
 EmpID:123, EmpName:Rama, Department:Facilities
 EmpID:124, EmpName:Lakshman, Department:Transport
 EmpID:125, EmpName:Ravan, Department:Packing
Elements after reversing: 
 EmpID:125, EmpName:Ravan, Department:Packing
 EmpID:124, EmpName:Lakshman, Department:Transport
 EmpID:123, EmpName:Rama, Department:Facilities
 
SujanM
sweekruth__17
Java-ArrayList
Java-Collections
Java-List-Programs
Picked
Reverse
Java
Java
Reverse
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.