Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

util.LinkedList: practical reference

util.LinkedList: practical reference: LinkedList<E> is a doubly linked list implementing List, Deque, and in Java 21 SequencedCollection; it permits null elements.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

java.util.LinkedList

LinkedList is a doubly linked list implementing List, Deque, and in Java 21 SequencedCollection; it permits null elements.

Use it mainly as a queue or deque with fast end operations when you also need the List interface.

Indexed access traverses from the nearer end and remains O(n), so frequent get(i) usually favors ArrayList.

It is not synchronized; iterators are fail-fast on a best-effort basis, and getFirst, getLast, removeFirst, and removeLast throw NoSuchElementException when empty.

The example exercises the documented element in a small deterministic program and compares exact standard output.

Examples

public class Main {
    public static void main(String[] args) throws Exception {
        var list = new java.util.LinkedList<Integer>();
        list.addFirst(1);
        list.addLast(2);
        System.out.println(list);
    }
}

Result

[1, 2]

The example exercises the documented element in a small deterministic program and compares exact standard output.

Sources