How to Reverse the Elements of a List in Java?

How to Reverse the Elements of a List in Java? | Step-by-Step Guide with Code Examples

Introduction

In Java, a List is a collection that maintains the order of elements and allows duplicate values. The need to reverse a list’s order arises often in different programming tasks. Java provides various ways to achieve this, such as using built-in methods in the Collections class or implementing custom approaches. This guide explains how to reverse the elements of a List in Java with step-by-step explanations and practical code examples.

Reversing a List in Java Using the Collections.reverse() Method

The easiest and most common way to reverse a List in Java is by using the Collections.reverse() method. This method modifies the original list by reversing its elements in place. The Collections class is part of the java.util package and contains a variety of useful methods for list manipulation.

Here’s an example of using Collections.reverse():


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

public class ListReversalExample {
    public static void main(String[] args) {
        List fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");
        fruits.add("Date");

        System.out.println("Original List: " + fruits);

        // Reversing the list using Collections.reverse()
        Collections.reverse(fruits);

        System.out.println("Reversed List: " + fruits);
    }
}

                

Output:

Original List: [Apple, Banana, Cherry, Date]
Reversed List: [Date, Cherry, Banana, Apple]
            

In this example, the original list of fruits is reversed using Collections.reverse() without creating a new list. This method is efficient and simple to use.

Reversing a List Using a for Loop

Another way to reverse a list in Java is by manually iterating through the list in reverse order and constructing a new list. This approach does not modify the original list but instead creates a new list with reversed elements.


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

public class ReverseUsingLoop {
    public static void main(String[] args) {
        List fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");
        fruits.add("Date");

        List reversedList = new ArrayList<>();

        for (int i = fruits.size() - 1; i >= 0; i--) {
            reversedList.add(fruits.get(i));
        }

        System.out.println("Original List: " + fruits);
        System.out.println("Reversed List: " + reversedList);
    }
}

                

Output:

Original List: [Apple, Banana, Cherry, Date]
Reversed List: [Date, Cherry, Banana, Apple]
            

In this example, we iterate through the list from the last element to the first and add each element to a new list. This method allows us to keep the original list intact while generating a reversed list.

Reversing a List Using Java Streams

For a more functional programming approach, you can use Java Streams to reverse the list. Java Streams, introduced in Java 8, allow a more declarative way of handling collections.


import java.util.*;
import java.util.stream.Collectors;

public class ReverseUsingStreams {
    public static void main(String[] args) {
        List fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");
        fruits.add("Date");

        List reversedList = fruits.stream()
                                          .sorted(Comparator.reverseOrder())
                                          .collect(Collectors.toList());

        System.out.println("Original List: " + fruits);
        System.out.println("Reversed List: " + reversedList);
    }
}

                

Output:

Original List: [Apple, Banana, Cherry, Date]
Reversed List: [Date, Cherry, Banana, Apple]
            

In this approach, we use the stream() method to create a stream from the list, apply a sorted() operation with a Comparator.reverseOrder(), and then collect the results into a new list. This provides a concise, modern way to reverse a list.

Reversing a List Using a Stack

A Stack is another data structure that can be used to reverse a list in Java. A Stack operates on a Last-In-First-Out (LIFO) basis, so we can push all the elements of the list onto a stack and then pop them out to create a reversed list.


import java.util.*;

public class ReverseUsingStack {
    public static void main(String[] args) {
        List fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");
        fruits.add("Date");

        Stack stack = new Stack<>();
        stack.addAll(fruits);

        List reversedList = new ArrayList<>();
        while (!stack.isEmpty()) {
            reversedList.add(stack.pop());
        }

        System.out.println("Original List: " + fruits);
        System.out.println("Reversed List: " + reversedList);
    }
}

                

Output:

Original List: [Apple, Banana, Cherry, Date]
Reversed List: [Date, Cherry, Banana, Apple]
            

In this example, we push all the elements of the list into a stack and then pop the elements to build a new list with the elements in reverse order.

Conclusion

Reversing a list in Java can be done in several ways depending on the specific use case and the performance requirements. The Collections.reverse() method is the most straightforward approach, but you can also implement custom methods using loops, streams, or stacks for more control. All of these methods are effective, and choosing the right one depends on your specific needs.

We hope this guide has helped you understand how to reverse the elements of a List in Java. Whether you’re using built-in methods or custom implementations, each approach can be valuable for different scenarios in your Java projects.

© 2025 Tech Interview Guide. All Rights Reserved.

Please follow and like us:

Leave a Comment