Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

util.SequencedCollection: practical reference

util.SequencedCollection: practical reference: SequencedCollection<E> defines a collection with a well-defined encounter order, operations at both ends, and a reverse-ordered view.

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

Syntax

java.util.SequencedCollection

SequencedCollection defines a collection with a well-defined encounter order, operations at both ends, and a reverse-ordered view.

Use this interface when an algorithm needs first and last elements plus traversal in either direction without committing to List or Deque.

addFirst, addLast, and removal are optional operations; the default add methods throw UnsupportedOperationException unless an implementation overrides them.

getFirst, getLast, removeFirst, and removeLast throw NoSuchElementException when empty; a modifiable reversed view writes through to its backing collection.

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 {
        java.util.SequencedCollection<Integer> values = new java.util.ArrayList<>(java.util.List.of(1, 2, 3));
        System.out.println(values.getFirst() + values.getLast());
    }
}

Result

4

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

Sources