Java : LinkedList with Examples

LinkedList (Java SE 18 & JDK 18) API Examples.
You will find code examples on most LinkedList methods.


Summary

Doubly-linked list implementation of the List and Deque interfaces. Implements all optional list operations, and permits all elements (including null).

Class diagram

final var list = new LinkedList<String>();
System.out.println(list); // []

list.add("aaa");
list.add("bbb");
list.add("ccc");
System.out.println(list); // [aaa, bbb, ccc]

System.out.println(list.get(0)); // aaa
System.out.println(list.get(1)); // bbb
System.out.println(list.get(2)); // ccc
final var list = new LinkedList<String>();
System.out.println(list); // []

list.add("xxx");
list.add("yyy");
list.add("zzz");
System.out.println(list); // [xxx, yyy, zzz]

System.out.println(list.removeFirst()); // xxx
System.out.println(list); // [yyy, zzz]

System.out.println(list.removeFirst()); // yyy
System.out.println(list); // [zzz]

System.out.println(list.removeFirst()); // zzz
System.out.println(list); // []

Fields declared in AbstractList

modCount

Please see the link below.

Constructors

LinkedList ()

Constructs an empty list.

final var list = new LinkedList<String>();
System.out.println(list); // []

list.add("aaa");
list.add("bbb");
list.add("ccc");
System.out.println(list); // [aaa, bbb, ccc]

LinkedList (Collection<? extends E> c)

Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

final var c = List.of("aaa", "bbb", "ccc");
System.out.println(c); // [aaa, bbb, ccc]

final var list = new LinkedList<>(c);
System.out.println(list); // [aaa, bbb, ccc]

Methods

void add (int index, E element)

Inserts the specified element at the specified position in this list.

final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]

{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    list.add(0, "A");
    System.out.println(list); // [A, a, b, c]
}
{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    list.add(1, "B");
    System.out.println(list); // [a, B, b, c]
}
{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    list.add(2, "C");
    System.out.println(list); // [a, b, C, c]
}
{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    list.add(3, "D");
    System.out.println(list); // [a, b, c, D]
}
{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    //list.add(4, "D"); // IndexOutOfBoundsException
}

boolean add (E e)

Appends the specified element to the end of this list.

This method is equivalent to addLast(E e).

boolean addAll (int index, Collection<? extends E> c)

Inserts all of the elements in the specified collection into this list, starting at the specified position.

final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]

{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.addAll(0, List.of("A1", "A2"))); // true
    System.out.println(list); // [A1, A2, a, b, c]
}
{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.addAll(1, List.of("B1", "B2", "B3"))); // true
    System.out.println(list); // [a, B1, B2, B3, b, c]
}
{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.addAll(2, List.of("C1", "C2", "C3", "C4"))); // true
    System.out.println(list); // [a, b, C1, C2, C3, C4, c]
}
{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.addAll(3, List.of())); // false
    System.out.println(list); // [a, b, c]
}
{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    //list.addAll(4, List.of("E")); // IndexOutOfBoundsException
}
final var src = List.of("a", "b", "c");
System.out.println(src);// [a, b, c]

final var list = new LinkedList<>(src);

System.out.println(list.addAll(3, Set.of("D1", "D2", "D3"))); // true
System.out.println(list); // [a, b, c, D3, D2, D1]

boolean addAll (Collection<? extends E> c)

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.

final var list = new LinkedList<String>();

System.out.println(list.addAll(List.of("a1"))); // true
System.out.println(list); // [a1]

System.out.println(list.addAll(List.of("b1", "b2"))); // true
System.out.println(list); // [a1, b1, b2]

System.out.println(list.addAll(List.of("c1", "c2", "c3"))); // true
System.out.println(list); // [a1, b1, b2, c1, c2, c3]

System.out.println(list.addAll(List.of())); // false
System.out.println(list); // [a1, b1, b2, c1, c2, c3]
final var list = new LinkedList<String>();
System.out.println(list); // []

System.out.println(list.addAll(Set.of("c1", "c2", "c3"))); // true
System.out.println(list); // [c3, c2, c1]

void addFirst (E e)

Inserts the specified element at the beginning of this list.

final var list = new LinkedList<String>();
System.out.println(list); // []

list.addFirst("aaa");
System.out.println(list); // [aaa]

list.addFirst("bbb");
System.out.println(list); // [bbb, aaa]

list.addFirst("ccc");
System.out.println(list); // [ccc, bbb, aaa]

void addLast (E e)

Appends the specified element to the end of this list.

final var list = new LinkedList<String>();
System.out.println(list); // []

list.addLast("aaa");
System.out.println(list); // [aaa]

list.addLast("bbb");
System.out.println(list); // [aaa, bbb]

list.addLast("ccc");
System.out.println(list); // [aaa, bbb, ccc]

void clear ()

Removes all of the elements from this list.

final var list = new LinkedList<String>();
list.add("a");
list.add("b");
list.add("c");

System.out.println(list); // [a, b, c]

list.clear();
System.out.println(list); // []

Object clone ()

Returns a shallow copy of this LinkedList.

final var list = new LinkedList<String>();
list.add("a");
list.add("b");
list.add("c");

System.out.println(list); // [a, b, c]

final var cloned = list.clone();
System.out.println(cloned); // [a, b, c]
System.out.println(cloned.getClass()); // class java.util.LinkedList

boolean contains (Object o)

Returns true if this list contains the specified element.

final var list = new LinkedList<String>();
Collections.addAll(list, "a", "b", "c");
System.out.println(list); // [a, b, c]

System.out.println(list.contains("")); // false
System.out.println(list.contains("a")); // true
System.out.println(list.contains("b")); // true
System.out.println(list.contains("c")); // true

System.out.println(list.contains("abc")); // false

Iterator<E> descendingIterator ()

Returns an iterator over the elements in this deque in reverse sequential order.

final var list = new LinkedList<>(List.of("aaa", "bbb", "ccc"));
System.out.println(list); // [aaa, bbb, ccc]

final var iterator = list.descendingIterator();

System.out.println("-- forEachRemaining --");
iterator.forEachRemaining(System.out::println);

// Result
// ↓
//-- forEachRemaining --
//ccc
//bbb
//aaa

E element ()

Retrieves, but does not remove, the head (first element) of this list.

final var list = new LinkedList<String>();
System.out.println(list); // []

list.addLast("aaa");
list.addLast("bbb");
list.addLast("ccc");

System.out.println(list.element()); // aaa
System.out.println(list); // [aaa, bbb, ccc]

System.out.println(list.removeFirst()); // aaa
System.out.println(list); // [bbb, ccc]

System.out.println(list.element()); // bbb
System.out.println(list); // [bbb, ccc]

System.out.println(list.removeFirst()); // bbb
System.out.println(list); // [ccc]

System.out.println(list.element()); // ccc
System.out.println(list); // [ccc]

System.out.println(list.removeFirst()); // ccc
System.out.println(list); // []

try {
    list.element();
} catch (NoSuchElementException e) {
    System.out.println(e);
}

// Result
// ↓
//java.util.NoSuchElementException

E get (int index)

Returns the element at the specified position in this list.

final var list = new LinkedList<String>();
Collections.addAll(list, "a", "b", "c");
System.out.println(list); // [a, b, c]

//list.get(-1); // IndexOutOfBoundsException
System.out.println(list.get(0)); // a
System.out.println(list.get(1)); // b
System.out.println(list.get(2)); // c
//list.get(3); // IndexOutOfBoundsException
final var list = new LinkedList<Integer>();
Collections.addAll(list, 10, 20, 30, 40);
System.out.println(list); // [10, 20, 30, 40]

System.out.println(list.get(0)); // 10
System.out.println(list.get(1)); // 20
System.out.println(list.get(2)); // 30
System.out.println(list.get(3)); // 40

E getFirst ()

Returns the first element in this list.

final var list = new LinkedList<String>();
System.out.println(list); // []

list.addLast("aaa");
list.addLast("bbb");
list.addLast("ccc");

System.out.println(list.getFirst()); // aaa
System.out.println(list); // [aaa, bbb, ccc]

System.out.println(list.removeFirst()); // aaa
System.out.println(list); // [bbb, ccc]

System.out.println(list.getFirst()); // bbb
System.out.println(list); // [bbb, ccc]

System.out.println(list.removeFirst()); // bbb
System.out.println(list); // [ccc]

System.out.println(list.getFirst()); // ccc
System.out.println(list); // [ccc]

System.out.println(list.removeFirst()); // ccc
System.out.println(list); // []

try {
    list.getFirst();
} catch (NoSuchElementException e) {
    System.out.println(e);
}

// Result
// ↓
//java.util.NoSuchElementException

E getLast ()

Returns the last element in this list.

final var list = new LinkedList<String>();
System.out.println(list); // []

list.addLast("aaa");
list.addLast("bbb");
list.addLast("ccc");

System.out.println(list.getLast()); // ccc
System.out.println(list); // [aaa, bbb, ccc]

System.out.println(list.removeLast()); // ccc
System.out.println(list); // [aaa, bbb]

System.out.println(list.getLast()); // bbb
System.out.println(list); // [aaa, bbb]

System.out.println(list.removeLast()); // bbb
System.out.println(list); // [aaa]

System.out.println(list.getLast()); // aaa
System.out.println(list); // [aaa]

System.out.println(list.removeLast()); // aaa
System.out.println(list); // []

try {
    list.getLast();
} catch (NoSuchElementException e) {
    System.out.println(e);
}

// Result
// ↓
//java.util.NoSuchElementException

int indexOf (Object o)

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

final var list = new LinkedList<String>();
Collections.addAll(list, "a", "b", "c", "a", "b", "c");
System.out.println(list); // [a, b, c, a, b, c]

System.out.println(list.indexOf("")); // -1
System.out.println(list.indexOf("a")); // 0
System.out.println(list.indexOf("b")); // 1
System.out.println(list.indexOf("c")); // 2
System.out.println(list.indexOf("d")); // -1
System.out.println(list.indexOf("abc")); // -1

int lastIndexOf (Object o)

Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

final var list = new LinkedList<String>();
Collections.addAll(list, "a", "b", "c", "a", "b", "c");
System.out.println(list); // [a, b, c, a, b, c]

System.out.println(list.lastIndexOf("")); // -1
System.out.println(list.lastIndexOf("a")); // 3
System.out.println(list.lastIndexOf("b")); // 4
System.out.println(list.lastIndexOf("c")); // 5
System.out.println(list.lastIndexOf("d")); // -1
System.out.println(list.lastIndexOf("abc")); // -1

ListIterator<E> listIterator (int index)

Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.

final var list = new LinkedList<String>();
Collections.addAll(list, "aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]

{
    final var ret = list.listIterator(0);
    ret.forEachRemaining(System.out::println);

    // Result
    // ↓
    //aaa
    //bbb
    //ccc
}
{
    final var ret = list.listIterator(1);
    ret.forEachRemaining(System.out::println);

    // Result
    // ↓
    //bbb
    //ccc
}
{
    final var ret = list.listIterator(2);
    ret.forEachRemaining(System.out::println);

    // Result
    // ↓
    //ccc
}

boolean offer (E e)

Adds the specified element as the tail (last element) of this list.

final var list = new LinkedList<String>();
System.out.println(list); // []

System.out.println(list.offer("aaa")); // true
System.out.println(list); // [aaa]

System.out.println(list.offer("bbb")); // true
System.out.println(list); // [aaa, bbb]

System.out.println(list.offer("ccc")); // true
System.out.println(list); // [aaa, bbb, ccc]

boolean offerFirst (E e)

Inserts the specified element at the front of this list.

final var list = new LinkedList<String>();
System.out.println(list); // []

System.out.println(list.offerFirst("aaa")); // true
System.out.println(list); // [aaa]

System.out.println(list.offerFirst("bbb")); // true
System.out.println(list); // [bbb, aaa]

System.out.println(list.offerFirst("ccc")); // true
System.out.println(list); // [ccc, bbb, aaa]

boolean offerLast (E e)

Inserts the specified element at the end of this list.

final var list = new LinkedList<String>();
System.out.println(list); // []

System.out.println(list.offerLast("aaa")); // true
System.out.println(list); // [aaa]

System.out.println(list.offerLast("bbb")); // true
System.out.println(list); // [aaa, bbb]

System.out.println(list.offerLast("ccc")); // true
System.out.println(list); // [aaa, bbb, ccc]

E peek ()

Retrieves, but does not remove, the head (first element) of this list.

final var list = new LinkedList<String>();
System.out.println(list); // []

list.addLast("aaa");
list.addLast("bbb");
list.addLast("ccc");

System.out.println(list.peek()); // aaa
System.out.println(list); // [aaa, bbb, ccc]

System.out.println(list.pollFirst()); // aaa
System.out.println(list); // [bbb, ccc]

System.out.println(list.peek()); // bbb
System.out.println(list); // [bbb, ccc]

System.out.println(list.pollFirst()); // bbb
System.out.println(list); // [ccc]

System.out.println(list.peek()); // ccc
System.out.println(list); // [ccc]

System.out.println(list.pollFirst()); // ccc
System.out.println(list); // []

System.out.println(list.peek()); // null
System.out.println(list); // []

E peekFirst ()

Retrieves, but does not remove, the first element of this list, or returns null if this list is empty.

final var list = new LinkedList<String>();
System.out.println(list); // []

list.addLast("aaa");
list.addLast("bbb");
list.addLast("ccc");

System.out.println(list.peekFirst()); // aaa
System.out.println(list); // [aaa, bbb, ccc]

System.out.println(list.pollFirst()); // aaa
System.out.println(list); // [bbb, ccc]

System.out.println(list.peekFirst()); // bbb
System.out.println(list); // [bbb, ccc]

System.out.println(list.pollFirst()); // bbb
System.out.println(list); // [ccc]

System.out.println(list.peekFirst()); // ccc
System.out.println(list); // [ccc]

System.out.println(list.pollFirst()); // ccc
System.out.println(list); // []

System.out.println(list.peekFirst()); // null
System.out.println(list); // []

E peekLast ()

Retrieves, but does not remove, the last element of this list, or returns null if this list is empty.

final var list = new LinkedList<String>();
System.out.println(list); // []

list.addLast("aaa");
list.addLast("bbb");
list.addLast("ccc");

System.out.println(list.peekLast()); // ccc
System.out.println(list); // [aaa, bbb, ccc]

System.out.println(list.pollLast()); // ccc
System.out.println(list); // [aaa, bbb]

System.out.println(list.peekLast()); // bbb
System.out.println(list); // [aaa, bbb]

System.out.println(list.pollLast()); // bbb
System.out.println(list); // [aaa]

System.out.println(list.peekLast()); // aaa
System.out.println(list); // [aaa]

System.out.println(list.pollLast()); // aaa
System.out.println(list); // []

System.out.println(list.peekLast()); // null
System.out.println(list); // []

E poll ()

Retrieves and removes the head (first element) of this list.

final var list = new LinkedList<String>();
System.out.println(list); // []

list.add("aaa");
list.add("bbb");
list.add("ccc");

System.out.println(list); // [aaa, bbb, ccc]

System.out.println(list.poll()); // aaa
System.out.println(list); // [bbb, ccc]

System.out.println(list.poll()); // bbb
System.out.println(list); // [ccc]

System.out.println(list.poll()); // ccc
System.out.println(list); // []

System.out.println(list.poll()); // null
System.out.println(list); // []

E pollFirst ()

Retrieves and removes the first element of this list, or returns null if this list is empty.

final var list = new LinkedList<String>();
System.out.println(list); // []

list.add("aaa");
list.add("bbb");
list.add("ccc");

System.out.println(list); // [aaa, bbb, ccc]

System.out.println(list.pollFirst()); // aaa
System.out.println(list); // [bbb, ccc]

System.out.println(list.pollFirst()); // bbb
System.out.println(list); // [ccc]

System.out.println(list.pollFirst()); // ccc
System.out.println(list); // []

System.out.println(list.pollFirst()); // null
System.out.println(list); // []

E pollLast ()

Retrieves and removes the last element of this list, or returns null if this list is empty.

final var list = new LinkedList<String>();
System.out.println(list); // []

list.add("aaa");
list.add("bbb");
list.add("ccc");

System.out.println(list); // [aaa, bbb, ccc]

System.out.println(list.pollLast()); // ccc
System.out.println(list); // [aaa, bbb]

System.out.println(list.pollLast()); // bbb
System.out.println(list); // [aaa]

System.out.println(list.pollLast()); // aaa
System.out.println(list); // []

System.out.println(list.pollLast()); // null
System.out.println(list); // []

E pop ()

Pops an element from the stack represented by this list.

This method is equivalent to removeFirst().

void push (E e)

Pushes an element onto the stack represented by this list.

This method is equivalent to addFirst(E e).

E remove ()

Retrieves and removes the head (first element) of this list.

final var list = new LinkedList<String>();
System.out.println(list); // []

list.add("aaa");
list.add("bbb");
list.add("ccc");

System.out.println(list); // [aaa, bbb, ccc]

System.out.println(list.remove()); // aaa
System.out.println(list); // [bbb, ccc]

System.out.println(list.remove()); // bbb
System.out.println(list); // [ccc]

System.out.println(list.remove()); // ccc
System.out.println(list); // []

try {
    list.remove();
} catch (NoSuchElementException e) {
    System.out.println(e);
}

// Result
// ↓
//java.util.NoSuchElementException

E remove (int index)

Removes the element at the specified position in this list.

final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]

{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.remove(0)); // a
    System.out.println(list); // [b, c]
}
{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.remove(1)); // b
    System.out.println(list); // [a, c]
}
{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.remove(2)); // c
    System.out.println(list); // [a, b]
}
{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    //list.remove(3); // IndexOutOfBoundsException
}

boolean remove (Object o)

Removes the first occurrence of the specified element from this list, if it is present.

final var src = List.of("a", "b", "a", "b", "A", "B");
System.out.println(src); // [a, b, a, b, A, B]

{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.remove("")); // false
    System.out.println(list); // [a, b, a, b, A, B]
}
{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.remove("a")); // true
    System.out.println(list); // [b, a, b, A, B]
}
{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.remove("b")); // true
    System.out.println(list); // [a, a, b, A, B]
}
{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.remove("A")); // true
    System.out.println(list); // [a, b, a, b, B]
}
{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.remove("B")); // true
    System.out.println(list); // [a, b, a, b, A]
}
{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.remove("C")); // false
    System.out.println(list); // [a, b, a, b, A, B]
}

E removeFirst ()

Removes and returns the first element from this list.

final var list = new LinkedList<String>();
System.out.println(list); // []

list.add("aaa");
list.add("bbb");
list.add("ccc");

System.out.println(list); // [aaa, bbb, ccc]

System.out.println(list.removeFirst()); // aaa
System.out.println(list); // [bbb, ccc]

System.out.println(list.removeFirst()); // bbb
System.out.println(list); // [ccc]

System.out.println(list.removeFirst()); // ccc
System.out.println(list); // []

try {
    list.removeFirst();
} catch (NoSuchElementException e) {
    System.out.println(e);
}

// Result
// ↓
//java.util.NoSuchElementException

boolean removeFirstOccurrence (Object o)

Removes the first occurrence of the specified element in this list (when traversing the list from head to tail).

final var list = new LinkedList<>(List.of("aaa", "BBB", "aaa", "BBB", "ccc"));
System.out.println(list); // [aaa, BBB, aaa, BBB, ccc]

System.out.println(list.removeFirstOccurrence("aaa")); // true
System.out.println(list); // [BBB, aaa, BBB, ccc]

System.out.println(list.removeFirstOccurrence("BBB")); // true
System.out.println(list); // [aaa, BBB, ccc]

System.out.println(list.removeFirstOccurrence("XXX")); // false
System.out.println(list); // [aaa, BBB, ccc]

E removeLast ()

Removes and returns the last element from this list.

final var list = new LinkedList<String>();
System.out.println(list); // []

list.add("aaa");
list.add("bbb");
list.add("ccc");

System.out.println(list); // [aaa, bbb, ccc]

System.out.println(list.removeLast()); // ccc
System.out.println(list); // [aaa, bbb]

System.out.println(list.removeLast()); // bbb
System.out.println(list); // [aaa]

System.out.println(list.removeLast()); // aaa
System.out.println(list); // []

try {
    list.removeLast();
} catch (NoSuchElementException e) {
    System.out.println(e);
}

// Result
// ↓
//java.util.NoSuchElementException

boolean removeLastOccurrence (Object o)

Removes the last occurrence of the specified element in this list (when traversing the list from head to tail).

final var list = new LinkedList<>(List.of("aaa", "BBB", "aaa", "BBB", "ccc"));
System.out.println(list); // [aaa, BBB, aaa, BBB, ccc]

System.out.println(list.removeLastOccurrence("BBB")); // true
System.out.println(list); // [aaa, BBB, aaa, ccc]

System.out.println(list.removeLastOccurrence("aaa")); // true
System.out.println(list); // [aaa, BBB, ccc]

System.out.println(list.removeLastOccurrence("XXX")); // false
System.out.println(list); // [aaa, BBB, ccc]

E set (int index, E element)

Replaces the element at the specified position in this list with the specified element.

final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]

{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.set(0, "A")); // a
    System.out.println(list); // [A, b, c]
}
{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.set(1, "B")); // b
    System.out.println(list); // [a, B, c]
}
{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.set(2, "C")); // c
    System.out.println(list); // [a, b, C]
}
{
    final var list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    //list.set(3, "D"); // IndexOutOfBoundsException
}

int size ()

Returns the number of elements in this list.

final var list = new LinkedList<String>();
System.out.println(list); // []
System.out.println(list.size()); // 0

list.add("a");

System.out.println(list); // [a]
System.out.println(list.size()); // 1

list.add("b");
list.add("c");
list.add("d");

System.out.println(list); // [a, b, c, d]
System.out.println(list.size()); // 4

Spliterator<E> spliterator ()

Creates a late-binding and fail-fast Spliterator over the elements in this list.

final var list = new LinkedList<String>();
Collections.addAll(list, "aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]

final var spliterator = list.spliterator();
spliterator.forEachRemaining(System.out::println);

// Result
// ↓
//aaa
//bbb
//ccc

Object[] toArray ()

Returns an array containing all of the elements in this list in proper sequence (from first to last element).

final var list = new LinkedList<String>();
Collections.addAll(list, "a", "b", "c");
System.out.println(list); // [a, b, c]

final Object[] array = list.toArray();
System.out.println(Arrays.toString(array)); // [a, b, c]

<T> T[] toArray (T[] a)

Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.

final var list = new LinkedList<String>();
Collections.addAll(list, "a", "b", "c");
System.out.println(list); // [a, b, c]

final String[] array = list.toArray(new String[0]);
System.out.println(Arrays.toString(array)); // [a, b, c]
final var list = new LinkedList<String>();
Collections.addAll(list, "a", "b", "c");
System.out.println(list); // [a, b, c]

{
    final String[] array = new String[3];
    System.out.println(Arrays.toString(array)); // [null, null, null]

    final var ret = list.toArray(array);
    System.out.println(Arrays.toString(array)); // [a, b, c]
    System.out.println(Arrays.toString(ret)); // [a, b, c]
}
{
    final String[] array = new String[5];
    System.out.println(Arrays.toString(array)); // [null, null, null, null, null]

    final var ret = list.toArray(array);
    System.out.println(Arrays.toString(array)); // [a, b, c, null, null]
    System.out.println(Arrays.toString(ret)); // [a, b, c, null, null]
}
{
    final String[] array = new String[1];
    System.out.println(Arrays.toString(array)); // [null]

    final var ret = list.toArray(array);
    System.out.println(Arrays.toString(array)); // [null]
    System.out.println(Arrays.toString(ret)); // [a, b, c]
}

Methods declared in AbstractSequentialList

iterator

Please see the link below.

Methods declared in AbstractList

equals, hashCode, listIterator, removeRange, subList

Please see the link below.

Methods declared in AbstractCollection

containsAll, isEmpty, removeAll, retainAll, toString

Please see the link below.

Methods declared in Collection

parallelStream, removeIf, stream, toArray

Please see the link below.

Methods declared in Deque

iterator

Please see the link below.

Methods declared in Iterable

forEach

Please see the link below.

Methods declared in List

containsAll, equals, hashCode, isEmpty, iterator, listIterator, removeAll, replaceAll, retainAll, sort, subList

Please see the link below.


Related posts

To top of page