How to Reverse ArrayList in Java with Example

In Java we have different ways to reverse arraylist . In earlier post learn arraylist in java  that it is resistible array backed list and maintain insertion order. We can reverse an arraylist by using following ways

  1. Using Collections.reverse() method.
  2. Using Recursion
  3. Using add() + get() method – extra space require
  4. Using add() + remove() method – No extra space require                                                                    

    1. Using Collections.reverse() method –  

    Collection.reverse() method can use for reverse a list in java .

Sysntax –

public static void reverse(List<?> list) {
 
   }

This method reverses the order of the elements in the specified list.This method runs in linear time.
It takes list as parameter , whose elements are to be reversed.It throws UnsupportedOperationException – if the given list or its list-iterator does not support the set operation.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ArrayListReverseByCollections {

	public static void main(String[] args) {
		List<String> languages = new ArrayList<>(
                Arrays.asList("PHP", "JAVA", "Python"));
		
		   //print list
		    System.out.println("element of list ="+ languages);
		   /*    
		    Reverses the order of the elements in the specified list.
          */
          Collections.reverse(languages);
          //print reverse list
          System.out.println("element of reverse list ="+ languages);	
		
	}
	
	
}

Output:

element of list =[PHP, JAVA, Python]
element of reverse list =[Python, JAVA, PHP]

 2. Reverse Arraylist Using Recursion

Now we will reverse arraylist using recursion

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ReverseListByRecursion {

	
	
	 public static<String> void reverseList(List<String> list)
	    {
	        // Base Case: list is empty or only one element is left
	        if (list == null || list.size() <= 1)
	            return;
	 
	        // remove first element from list
	        String value = list.remove(0);
	 
	        // recur for remaining items of list
	        reverseList(list);
	 
	        // insert the top element back to list after recurse for remaining items
	        list.add(value);
	    }
	 
	public static void main(String[] args) {
		List<String> languages = new ArrayList<>(
                Arrays.asList("PHP", "JAVA", "Python"));
          reverseList(languages);
          System.out.println(languages);	
		
	}
}

Output :

[Python, JAVA, PHP]

3. Using add() + get() method – extra space require

We will write our own method , in this we reverse arraylist in java using for loop  , we read list element from last to first one by one and set to new arraylist.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ReverseListByMethod {

	public static void main(String[] args) {
		List<String> languages = new ArrayList<>(
                Arrays.asList("PHP", "JAVA", "Python"));
		
		ArrayList<String> reverseList = new ArrayList<String>();
		
		for (int i = languages.size() -1 ; i > -1 ; i--) {
			reverseList.add( languages.get(i));
		}
		
		//print reverse list
		System.out.println("reverse list is ="+ reverseList);
		
		
	}
}
Output :
reverse list is =[Python, JAVA, PHP]

4. Using add() + remove() method – No extra space require

In this we will use add and remove method together for reverse list and there is no need for extra space
Let’s see this trick by code
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ReverseList {
	
	public static void main(String[] args)
	{
		List<String> languages = new ArrayList<>(
                Arrays.asList("PHP", "JAVA", "Python"));
		for (int i = 0, j = languages.size() - 1; i < j; i++) {
			languages.add(i, languages.remove(j));
		}

		System.out.println(languages);
	}	

}
Output :
[Python, JAVA, PHP]

Leave a Reply

Your email address will not be published. Required fields are marked *

+ 7 = 8