ArrayList Reverse in Java
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.
Java Program
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
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.
- 1The list starts as
[A, B, C, D], built by fouradd()calls in that order. - 2
Collections.reverse(letters)walks the list from both ends toward the middle, swapping each matched pair of positions. - 3For a 4-element list, that's two swaps: index 0 with index 3, and index 1 with index 2.
- 4The list is modified in place — no new list is created, and
lettersitself now holds the reversed order.
[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.
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
Approach 2: Java 8
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
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.
- 1
IntStream.range(0, letters.size())produces the indices0throughsize() - 1, in ascending order. - 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
.collect(Collectors.toList())gathers those mirrored reads into a new List, in reversed order. - 4Unlike
Collections.reverse(), the originalletterslist is never modified —reversedis a completely separate list.
[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.
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.