Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Java 21 List endpoint methods: availability is not mutability

Last updated: 5 Oct 20264 min read
tutorial
IntermediateBy AITrove Editorial

Java 21 List exposes first and last endpoint methods, but whether an endpoint can be changed still depends on the list implementation.

Separate interface shape from operation support

A LinkedList supports getFirst, getLast, addFirst, and removeLast. A List created with List.of has the same endpoint method names but rejects mutation. Code accepting List<String> cannot infer modifiability from the interface alone. Decide whether the caller owns a mutable list before attempting an edit.

Empty endpoints have a different failure mode: getFirst and removeLast throw NoSuchElementException on an empty list. For a queue-like contract in which absence is normal, Deque polling can express that choice, subject to its null-element policy.

Preserve the release boundary

These List endpoint methods are a Java 21 surface. A program compiled with --release 8 cannot call them even though LinkedList had similarly named methods earlier through Deque. The declared type controls which methods are visible to the compiler.

Reversed views invert endpoint orientation. Test each operation against the collection contract instead of assuming an English description like first means the original head.

Working program

Java
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;

public class DispatchListEndpoints {
    public static void main(String[] args) {
        List<String> mutable = new LinkedList<>(List.of("seal-47", "ship-82"));
        mutable.addFirst("load-15");
        System.out.println(mutable.getFirst() + ":" + mutable.getLast());
        System.out.println(mutable.removeLast());
        try { List.of("audit-20").addFirst("intake-35"); }
        catch (UnsupportedOperationException rejected) { System.out.println("immutable edit rejected"); }
        try { new LinkedList<String>().getFirst(); }
        catch (NoSuchElementException absent) { System.out.println("empty endpoint rejected"); }
    }
}

Output

Output
load-15:ship-82
ship-82
immutable edit rejected
empty endpoint rejected

Cost and ownership

The endpoint methods avoid an explicit numeric index, but their cost depends on the concrete List. LinkedList endpoints are O(1); using a generic List interface does not promise that cost or mutation support.

Common Mistakes

  • Do not assume List.addFirst is supported just because it compiles.
  • Do not call getFirst on an empty list without an absence policy.
  • Do not confuse the compile-time List type with a specific LinkedList implementation.

Read next

Java 21 sequenced collections: endpoints and reversed views, Java LinkedList: operations, internals and failure cases, Java Deque empty operations: choose return values or exceptions, Java 21 LinkedList.reversed: write-through order and endpoint edits.

java
collections
sequenced-list-endpoint-contract
Storage details