Java ProgramsCollectionsArrayList Sorting

ArrayList Sorting in Java

beginner·  Collections  ·  List

Problem

Collections.sort() rearranges a list in place according to its elements' natural ordering — ascending for numbers, alphabetical for strings — with no comparator needed.

Given an ArrayList of integers in no particular order, sort it into ascending order.

Input
[42, 17, 8, 23]
Output
Sorted: [8, 17, 23, 42]

Java Program

Java
import java.util.ArrayList; import java.util.Collections; public class ArrayListSorting { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(42); numbers.add(17); numbers.add(8); numbers.add(23); Collections.sort(numbers); // rearranges numbers in place, ascending System.out.println("Sorted: " + numbers); } }

Output

Sorted: [8, 17, 23, 42]

Core Logic

Collections.sort() rearranges the list's existing elements directly, using each Integer's natural ascending order, without building a separate sorted copy.

How It Works
  1. 1The list starts as [42, 17, 8, 23], built by four add() calls in that order.
  2. 2Collections.sort(numbers) reorders the list's elements in place, comparing them by their natural ordering — ascending for Integer.
  3. 3No comparator is passed in, so the sort relies on Integer's own compareTo(), which orders smaller numbers first.
  4. 4After the call returns, numbers itself has been rearranged — the sort doesn't produce a new list.
Sorting [42, 17, 8, 23] rearranges it into [8, 17, 23, 42], smallest to largest.
💡

Key Point: Collections.sort() modifies the list it's given — the original unsorted order is gone once the call returns, unlike the stream-based approach, which builds a new sorted list and leaves the original untouched.

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

Why: Collections.sort() uses a Timsort-derived merge sort, which runs in O(n log n) comparisons and needs a temporary working array proportional to the list's size.

Key Concepts

Collections.sort()natural orderingin-place sort

Approach 2: Java 8

Java
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class ArrayListSortingStream { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(42); numbers.add(17); numbers.add(8); numbers.add(23); // Produces a new sorted list — the original numbers list is untouched List<Integer> sorted = numbers.stream().sorted().collect(Collectors.toList()); System.out.println("Sorted: " + sorted); } }

Output

Sorted: [8, 17, 23, 42]

Core Logic

A stream can sort declaratively, producing a brand-new sorted list instead of rearranging the original in place.

How It Works
  1. 1numbers.stream() opens a stream over the original, unsorted list.
  2. 2.sorted() orders the stream's elements using their natural ordering, the same rule Collections.sort() uses.
  3. 3.collect(Collectors.toList()) gathers the sorted elements into a brand-new List<Integer>.
  4. 4The original numbers list is never touched — sorted is a separate object.
Streaming [42, 17, 8, 23] through .sorted() produces the same [8, 17, 23, 42] result, but as a new list.
💡

Key Point: Because streams don't mutate their source, the original list stays in its original order after this call — useful when the unsorted order still needs to be available elsewhere in the program.

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

Why: sorted() performs the same O(n log n) comparison-based sort internally, and collect() materializes a full new list of size n rather than reusing the original's storage.

Key Concepts

Streamsorted()Collectors.toList()

Related Programs