ArrayList
Java
Coding Tutorial
Data Structures
Reverse Operation

What is the Simplest Way to Reverse an ArrayList?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Reversing an ArrayList is a common Java task, and the simplest answer is usually Collections.reverse(list). The important follow-up question is whether you want to reverse the original list in place or produce a new reversed list while leaving the original unchanged.

The Simplest In-Place Solution

The Java standard library already provides the most direct tool for this job.

java
1import java.util.ArrayList;
2import java.util.Arrays;
3import java.util.Collections;
4
5public class ReverseExample {
6    public static void main(String[] args) {
7        ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
8        Collections.reverse(numbers);
9        System.out.println(numbers);
10    }
11}

Output:

text
[5, 4, 3, 2, 1]

Collections.reverse is simple because it is already implemented, tested, and readable. It reverses the list in place, so the original list object is modified rather than copied.

When In-Place Reversal Is the Right Choice

In-place reversal is ideal when:

  • you no longer need the original order
  • the list is mutable
  • readability matters more than custom logic

For most application code, this is the correct answer. There is no advantage in manually swapping elements unless you need special behavior.

Creating a Reversed Copy Instead

Sometimes mutating the original list is a bug. If other code still depends on the original ordering, make a copy first and reverse the copy.

java
1import java.util.ArrayList;
2import java.util.Arrays;
3import java.util.Collections;
4import java.util.List;
5
6public class ReverseCopyExample {
7    public static void main(String[] args) {
8        List<String> original = Arrays.asList("a", "b", "c", "d");
9        List<String> copy = new ArrayList<>(original);
10
11        Collections.reverse(copy);
12
13        System.out.println("original = " + original);
14        System.out.println("reversed = " + copy);
15    }
16}

That produces a reversed result while preserving the original input.

Manual Reversal With a Loop

A manual swap loop is useful mainly for learning or for implementing a more specialized reverse operation.

java
1import java.util.ArrayList;
2import java.util.Arrays;
3
4public class ManualReverseExample {
5    public static void main(String[] args) {
6        ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(10, 20, 30, 40));
7
8        for (int i = 0; i < numbers.size() / 2; i++) {
9            int j = numbers.size() - 1 - i;
10            Integer temp = numbers.get(i);
11            numbers.set(i, numbers.get(j));
12            numbers.set(j, temp);
13        }
14
15        System.out.println(numbers);
16    }
17}

This does the same job as Collections.reverse, but it is longer and easier to get wrong. In normal Java code, the library call is still better.

Reverse Order Versus Reverse Sorting

Developers sometimes mix up two different ideas:

  • reversing the current order of a list
  • sorting a list in descending order

These are not the same. Reversal preserves the existing sequence but flips it. Descending sort rearranges elements based on their values.

java
1import java.util.ArrayList;
2import java.util.Arrays;
3import java.util.Collections;
4
5public class ReverseVsSort {
6    public static void main(String[] args) {
7        ArrayList<Integer> values = new ArrayList<>(Arrays.asList(3, 1, 4, 2));
8
9        ArrayList<Integer> reversed = new ArrayList<>(values);
10        Collections.reverse(reversed);
11
12        ArrayList<Integer> sortedDesc = new ArrayList<>(values);
13        sortedDesc.sort(Collections.reverseOrder());
14
15        System.out.println("reversed   = " + reversed);
16        System.out.println("sortedDesc = " + sortedDesc);
17    }
18}

Understanding that distinction prevents subtle bugs in business logic.

Performance Notes

Collections.reverse runs in linear time because it swaps corresponding elements from both ends of the list. That is optimal for reversal. There is no hidden benefit to replacing it with a stream-based solution for this task.

If you are reversing a LinkedList, the method still works because it operates on the List interface, but performance characteristics of indexed access differ from ArrayList. The article title is about ArrayList, so the standard method remains the cleanest fit.

Common Pitfalls

The most common mistake is forgetting that Collections.reverse mutates the list you pass in. If callers expect the original order later, copy the list first.

Another mistake is using streams for a job that the collections library already solves directly. A more “functional” solution is not automatically better if it is harder to read.

Developers also sometimes confuse reversal with descending sort. Those operations can produce different results from the same input.

Summary

  • The simplest way to reverse an ArrayList is Collections.reverse(list).
  • That method modifies the original list in place.
  • If you need to keep the original order, reverse a copy instead.
  • Manual swapping works but is usually less readable than the standard library method.
  • Reversing a list is not the same as sorting it in descending order.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.