Java ProgramsCollectionsArrayList Reverse

ArrayList Reverse in Java

beginner·  Collections  ·  List

Problem

Collections.reverse() flips a list's element order in place, swapping symmetric pairs from opposite ends until they meet in the middle.

Given an ArrayList of letters in one order, reverse it so the last element becomes the first.

Input
[A, B, C, D]
Output
Reversed: [D, C, B, A]

Java Program

Java
import java.util.ArrayList; import java.util.Collections; public class ArrayListReverse { public static void main(String[] args) { ArrayList<String> letters = new ArrayList<>(); letters.add("A"); letters.add("B"); letters.add("C"); letters.add("D"); Collections.reverse(letters); // swaps matched pairs from both ends inward System.out.println("Reversed: " + letters); } }

Output

Reversed: [D, C, B, A]

Core Logic

Collections.reverse() swaps the first element with the last, the second with the second-to-last, and so on, working inward until the whole list is flipped.

How It Works
  1. 1The list starts as [A, B, C, D], built by four add() calls in that order.
  2. 2Collections.reverse(letters) walks the list from both ends toward the middle, swapping each matched pair of positions.
  3. 3For a 4-element list, that's two swaps: index 0 with index 3, and index 1 with index 2.
  4. 4The list is modified in place — no new list is created, and letters itself now holds the reversed order.
Reversing [A, B, C, D] swaps A/D and B/C, producing [D, C, B, A].
💡

Key Point: Like Collections.sort(), Collections.reverse() mutates the list it's given — if the original order still needs to be kept somewhere, copy the list first with new ArrayList<>(letters) before reversing.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: Reversing an n-element list takes exactly n/2 swaps, each touching two positions directly, with no extra storage beyond a temporary variable for the swap itself.

Key Concepts

Collections.reverse()in-place reversalList

Approach 2: Java 8

Java
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; public class ArrayListReverseStream { public static void main(String[] args) { ArrayList<String> letters = new ArrayList<>(); letters.add("A"); letters.add("B"); letters.add("C"); letters.add("D"); // Maps ascending indices to mirrored positions, building a brand-new reversed list List<String> reversed = IntStream.range(0, letters.size()) .mapToObj(i -> letters.get(letters.size() - 1 - i)) .collect(Collectors.toList()); System.out.println("Reversed: " + reversed); } }

Output

Reversed: [D, C, B, A]

Core Logic

Streaming the list's indices backward and mapping each one to its element builds a brand-new reversed list, instead of mutating the original in place.

How It Works
  1. 1IntStream.range(0, letters.size()) produces the indices 0 through size() - 1, in ascending order.
  2. 2.mapToObj(i -> letters.get(letters.size() - 1 - i)) maps each ascending index to the element at the mirrored position from the end, effectively reading the list backward.
  3. 3.collect(Collectors.toList()) gathers those mirrored reads into a new List, in reversed order.
  4. 4Unlike Collections.reverse(), the original letters list is never modified — reversed is a completely separate list.
For [A, B, C, D], index 0 maps to letters.get(3) (D), index 1 to letters.get(2) (C), and so on, collecting into [D, C, B, A].
💡

Key Point: This is a genuinely different behavior from Collections.reverse(), not just different syntax — the original list is left completely untouched, which matters if other code still holds a reference to it.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: mapToObj() performs one get() lookup per index — O(1) each on an ArrayList — for n indices, and collect() builds a new list to hold the n reversed elements, unlike Collections.reverse()'s in-place O(1)-extra-space swaps.

Key Concepts

IntStreammapToObj()Collectors.toList()

Related Programs