Java Program to Reverse a List


What is a Reverse List?

Reverse a list is an operation of swapping or interchanging the elements position into a particular list. While you are writing a code in Java, you can easily reverse the order of a certain flow. It is a conventional way of any programming language in computer science. The reverse () method is a collection of class which reverses the position of first element to last element. Last element will earn the first position itself after the termination of the process.

A list is an interface where the class method is denoted as a signature with no definition. The class will be implemented from this by which a method gets a particular definition. Today in this article, we will learn about how to reverse a list using Java conditions with different methods.

How to Reverse a List Using Java?

In this method, we have to mention a pointer to perform the reversing process on a linked list by changing nodes.

  • Reverse an ArrayList in Java can be used by a class reverse method aka Collections.reverse(). In this method the list of the array will be in a linear time with a time complexity of O(n). This method accepts a List type argument to execute a program.

  • Sometimes when you are writing a code using Java, it's needed to start the operation from the last element to reverse an array. By altering the position of first and last elements, it's needed to get a grip that the process will run until the mid-elements swap their positions.

  • Print the array in a reverse manner, the coder needs to use a for-loop to initiate the printing operation from the end of that particular set of the data aka array. It is a conventional way to reverse a list.

  • There are so many interfaces in Java by which you can reverse a List but the in-place reversal is a better option to save the machine memory.

Algorithm to Reverse a List

Here is the general algorithm to reverse a linked list by using Java −

  • Step 1 − Create a new arraylist.

  • Step 2 − Put some data as input by using add(E e) API.

  • Step 3 − Reverse those elements of the list and use invoke reverse(List list) API.

Syntax

import java.util.Collections; (the Java Package)
Collections.reverse(the class_obj);

Reverse() method of class collection suggests as iereversing the elements in which they can be sorted.

There are several methods to reverse a list using Java −

  • Approach 1 − Reverse Array Printing Collections.reverse() method

  • Approach 2 − Use for loop to reverse an array

  • Approach 3 − In Place Method to reverse an array

  • Approach 4 − By Java 8 stream API

  • Approach 5 − By ListIterator

Reverse Array Printing By Using Collections.reverse() Method

Collections.reverse() method is the most acceptable way to reverse a list using Java. The reverse method follows the syntax: public static void reverse(List<?> list), to perform the code.

Example

public class reverseclassArray {
   static void reverse(int a[], int n){
      int[] b = new int[n];
      int j = n;
         for (int i = 0; i < n; i++) {
         b[j - 1] = a[i];
         j = j - 1;
      }
      System.out.println("Here the reversed array is:");
      for (int k = 0; k < n; k++) {
         System.out.println(b[k]);
      }
   }
   public static void main(String[] args){
      int [] arr12 = {101, 202, 303, 404, 505};
      reverse (arr12, arr12.length);
   }
}

Output

Here the reversed array is: 
505
404
303
202
101

Reverse an Array Using for Loop

We can use a for loop to reverse an array. In this method a new array is injected with the existing array and by this it will be in a reverse manner.

Example

public class Main { 
   static void reverse_array(char char_array[], int a) { 
      char[] dest_array = new char[a]; 
      int j = a; 
      for (int i = 0; i < a; i++) { 
         dest_array[j - 1] = char_array[i]; 
         j = j - 1; 
      } 
      System.out.println("Reversed array from this operation is: "); 
      for (int r = 0; r < a; r++){ 
         System.out.print(dest_array[r] + " "); 
      } 
   } 
   public static void main(String[] args){ 
      char [] char_array = {'I','N','D','I','A'}; 
      System.out.println("Original array print after the operation: "); 
      for (int s = 0; s <char_array.length; s++) { 
         System.out.print(char_array[s] + " ");
      }
      System.out.println();
      reverse_array(char_array, char_array.length); 
   } 
}

Output

Original array print after the operation: 
I N D I A 
Reversed array from this operation is: 
A I D N I 

In Place Method to Reverse an Array

The operation can be done without using another separate type of array. Method can be followed by the swapping between the first and last data of the array.

Example

public class reverseArray {
   static void reverse(int a[], int n){
      int[] b = new int[n];
      int j = n;
      for (int i = 0; i < n; i++) {
         b[j - 1] = a[i];
         j = j - 1;
      }
      System.out.println("Reversed array is after the operation:");
      for (int l = 0; l < n; l++) {
         System.out.println(b[l]);
      }
   }
   public static void main(String[] args){
      int [] arr = {1000, 2000, 3000, 4000, 5000};
      reverse(arr, arr.length);
   }
}

Output

Reversed array is after the operation: 
5000
4000
3000
2000
1000

By Using Java 8 Stream API

Using the stream API, create an int stream which represents the indexes of the list.

Example

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class FavFashion{ 
   public static void main(String[] args) {
      List<String> clothesfavv = new ArrayList<>();
      clothesfavv.add("Raymond Shirt");
      clothesfavv.add("Impact Pants");
      clothesfavv.add("Socks of Champion");
      clothesfavv.add("Shoes By Bata");
      System.out.println("Before reversing the whole data:");
      System.out.println(clothesfavv);
      List reverseClothesfavv = IntStream.range(0, clothesfavv.size()).map(i -> clothesfavv.size() - 1-i).mapToObj(clothesfavv::get).collect(Collectors.toList());
      System.out.println("After reversing the whole data:");
      System.out.println(reverseClothesfavv);
   }
}

Output

Before reversing the whole data:
[Raymond Shirt, Impact Pants, Socks of Champion, Shoes By Bata]
After reversing the whole data:
[Shoes By Bata, Socks of Champion, Impact Pants, Raymond Shirt]

By List Iterator

The Java environment has an iterator class, and can be used to iterate different collections of the data.

Example

import java.util.*;
public class FashionCollector{
   public static void main(String[] args) {
      List<String> clothesstore2023 = new ArrayList<>();
      clothesstore2023.add("T-shirt Of Raymond");
      clothesstore2023.add("Pants By Impact");
      clothesstore2023.add("Socks Of Champion");
      clothesstore2023.add("Shoes Of Bata");
      List<String> reverseclothesstore2023 = new ArrayList<>();
      ListIterator<String> listIterator = clothesstore2023.listIterator(clothesstore2023.size());
      while(listIterator.hasPrevious()){
         String elemenString = listIterator.previous();
         reverseclothesstore2023.add(elemenString);
      }
      System.out.println("Before reversing the storage data:");
      System.out.println(clothesstore2023);
      System.out.println("After reversing the storage data:");
      System.out.println(reverseclothesstore2023);
   }
}

Output

Before reversing the storage data:
[T-shirt Of Raymond, Pants By Impact, Socks Of Champion, Shoes Of Bata]
After reversing the storage data:
[Shoes Of Bata, Socks Of Champion, Pants By Impact, T-shirt Of Raymond]

Conclusion

Thus, from the above discussion, we have found how to reverse a list using by using Java. After implementation of various coding methods. It's recommended to understand those approach carefully.

While we are trying to reverse a list using Java, there are several problems we can encounter. But here is the solution by which a coder can face those problems in smart way.

Updated on: 31-Mar-2023

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements