Skip to content

Chapter 11Part IV · Data and Memory

Arrays → Collections

3 min read4 runnable examples

In this chapter: what arrays are and why their size is fixed, two array traps every Java developer meets, and how the collections API grew from the Java 7 diamond to Java 21's sequenced collections.

Key points

  • An array has a fixed length, set at creation. Elements get default values. arr.length is a field.
  • A 2-D array is an array of arrays; rows can have different lengths.
  • Arrays are covariant: a String[] is an Object[], and storing the wrong type fails at run time with ArrayStoreException. Generic collections catch it at compile time.
  • Arrays.asList is fixed-size; List.of (Java 9) is unmodifiable and rejects null.
  • Java 21 sequenced collections added getFirst(), getLast(), addFirst() and reversed() to lists, ordered sets and ordered maps.

Arrays

code/ch11/ArrayBasics.java
import java.util.Arrays;

public class ArrayBasics {
    public static void main(String[] args) {
        int[] scores = new int[3];  // fixed size, filled with defaults
        String[] names = new String[2];
        System.out.println(Arrays.toString(scores) + " "
                + Arrays.toString(names));

        // array initializer
        int[] primes = {2, 3, 5, 7};
        // a field, not a method
        System.out.println("length = " + primes.length);

        // an array of arrays; rows can differ
        int[][] triangle = new int[3][];
        for (int r = 0; r < triangle.length; r++) {
            triangle[r] = new int[r + 1];
            Arrays.fill(triangle[r], r + 1);
        }
        System.out.println(Arrays.deepToString(triangle));

        // an anonymous array
        System.out.println(sum(new int[] {10, 20, 30}));

        try {
            System.out.println(primes[4]);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println(e.getMessage());
        }
    }

    static int sum(int[] values) {
        int total = 0;
        for (int v : values) total += v;
        return total;
    }
}
[0, 0, 0] [null, null]
length = 4
[[1], [2, 2], [3, 3, 3]]
60
Index 4 out of bounds for length 4

An array is an object that holds a fixed number of values of one type, stored side by side. It has two classic advantages: one name for many values, and fast access by index.

The classic rule

"Rule → Once an array's memory is allocated, either statically or dynamically, it can't be changed."

Correct: to "grow" an array you create a bigger one and copy (Arrays.copyOf). That's exactly what ArrayList does for you behind the scenes.

The classic rule

"Q) Why does array index start with 0?"

Because the index is an offset: element i sits at start + i × element size, so the first element is at offset 0. Java inherited the convention from C.

A few array facts that still trip people up:

  • int[] a and int a[] both compile; the first is the Java style.
  • new int[]{10, 20, 30} is an anonymous array, handy for passing values directly.
  • Out-of-range indexes throw ArrayIndexOutOfBoundsException. Since Java 11 the message reads "Index 4 out of bounds for length 4"; Java 7 printed just "4."
  • Arrays.toString prints one level; Arrays.deepToString prints nested arrays.

Trap: arrays are covariant

code/ch11/ArrayCovariance.java
public class ArrayCovariance {
    public static void main(String[] args) {
        Object[] things = new String[2];  // compiles: String[] is an Object[]
        things[0] = "fine";
        try {
            things[1] = 42;  // compiles too, but fails at run time
        } catch (ArrayStoreException e) {
            System.out.println("ArrayStoreException: " + e.getMessage());
        }
        // generics reject this at compile time:
        // List<Object> list = new ArrayList<String>();
    }
}
ArrayStoreException: java.lang.Integer

Because String[] is a subtype of Object[], the compiler accepts storing an Integer through an Object[] reference. The JVM checks every array store at run time and throws. Generics, added in Java 5, deliberately made List<String> not a List<Object>, so the same mistake is a compile error. This is one of the main reasons to prefer collections over arrays for objects.

The collections framework

Java 1.2's collections framework is still the foundation: List (ordered, indexed), Set (no duplicates), Map (key → value) and Queue/Deque, with implementations like ArrayList, HashSet, HashMap and ArrayDeque.

flowchart TD
    IT["Iterable"] --> C["Collection"]
    C --> L["List<br/>ArrayList, LinkedList"]
    C --> S["Set<br/>HashSet, LinkedHashSet, TreeSet"]
    C --> Q["Queue / Deque<br/>ArrayDeque, PriorityQueue"]
    SC["SequencedCollection<br/>(Java 21)"]
    C --> SC
    SC --> L
    SC --> D2["Deque"]
    SC --> SS["SequencedSet<br/>LinkedHashSet, TreeSet"]
    M["Map<br/>HashMap, TreeMap"] --> SM["SequencedMap (Java 21)<br/>LinkedHashMap, TreeMap"]

Fixed-size vs. unmodifiable

code/ch11/FixedAndImmutable.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FixedAndImmutable {
    static void attempt(String what, Runnable action) {
        try {
            action.run();
            System.out.println(what + ": ok");
        } catch (RuntimeException e) {
            System.out.println(what + ": " + e.getClass().getSimpleName());
        }
    }

    public static void main(String[] args) {
        // Java 1.2: fixed-size view of an array
        List<String> asList = Arrays.asList("a", "b");
        // Java 9: truly unmodifiable
        List<String> listOf = List.of("a", "b");
        // diamond <>: Java 7
        List<String> growable = new ArrayList<>(listOf);

        attempt("Arrays.asList set", () -> asList.set(0, "z"));
        attempt("Arrays.asList add", () -> asList.add("c"));
        attempt("List.of set     ", () -> listOf.set(0, "z"));
        attempt("List.of null    ", () -> List.of("a", null));
        attempt("ArrayList add   ", () -> growable.add("c"));
        System.out.println(asList + " " + listOf + " " + growable);
    }
}
Arrays.asList set: ok
Arrays.asList add: UnsupportedOperationException
List.of set     : UnsupportedOperationException
List.of null    : NullPointerException
ArrayList add   : ok
[z, b] [a, b] [a, b, c]
Factory Change elements? Add/remove? null allowed?
Arrays.asList(...) Yes (writes through to the array) No Yes
List.of(...) (Java 9) No No No
List.copyOf(...) (Java 10) No No No
new ArrayList<>(...) Yes Yes Yes
stream.toList() (Java 16) No No Yes

Migration tip

Replace Collections.unmodifiableList(Arrays.asList(...)) with List.of(...), but check for null elements first. List.of rejects them, and the old code allowed them.

Sequenced collections (Java 21)

For 25 years, getting the last element of a List meant list.get(list.size() - 1), and of a LinkedHashSet meant iterating through the whole thing. Java 21 added three interfaces that capture "has a defined order" (JEP 431):

code/ch11/Sequenced.java
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.SequencedMap;

public class Sequenced {
    public static void main(String[] args) {
        List<Integer> releases = new ArrayList<>(List.of(17, 21, 25));
        releases.addFirst(11);                          // Java 21
        releases.addLast(27);
        System.out.println(releases + " first=" + releases.getFirst()
                + " last=" + releases.getLast());
        System.out.println("reversed view: " + releases.reversed());

        var visited =
                new LinkedHashSet<>(List.of("Java 7", "Java 8", "Java 11"));
        System.out.println("last visited: " + visited.getLast());

        SequencedMap<String, Integer> lts = new LinkedHashMap<>();
        lts.put("Java 8", 2014);
        lts.put("Java 21", 2023);
        lts.put("Java 25", 2025);
        System.out.println("oldest: " + lts.firstEntry()
                + ", newest: " + lts.lastEntry());

        // Java 7 equivalents, for comparison
        System.out.println(releases.get(releases.size() - 1) + " "
                + new ArrayList<>(visited).get(visited.size() - 1));
    }
}
[11, 17, 21, 25, 27] first=11 last=27
reversed view: [27, 25, 21, 17, 11]
last visited: Java 11
oldest: Java 8=2014, newest: Java 25=2025
27 Java 11
  • SequencedCollection adds getFirst, getLast, addFirst, addLast, removeFirst, removeLast and reversed.
  • SequencedSet (for LinkedHashSet, TreeSet) and SequencedMap (for LinkedHashMap, TreeMap, with firstEntry, lastEntry, pollFirstEntry…) do the same for sets and maps.
  • reversed() returns a view, not a copy, so it costs nothing.

Go deeper in the complete edition

This page is the focused edition. The complete edition of this chapter on GitHub adds the deep dives, puzzles, exercises and interview questions, with every sample ready to run.

Read the complete chapter →

Summary

  • Arrays have fixed size and run-time type checks on stores; collections are flexible and type-checked at compile time.
  • Arrays.asList is fixed-size; List.of/copyOf are unmodifiable and null-hostile.
  • The diamond (7), Map defaults (8), collection factories (9) and var (10) cut collection boilerplate.
  • Java 21's sequenced collections gave ordered collections first/last/reverse operations.

Next: Chapter 12 follows those objects to the end of their lives: garbage collection, and how Java's collectors changed from Java 7 to Java 27.