Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

util.SequencedSet: practical reference

util.SequencedSet: practical reference: SequencedSet<E> combines Set uniqueness with a defined encounter order, end operations, and a reversed view whose type remains SequencedSet.

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

Syntax

java.util.SequencedSet

SequencedSet combines Set uniqueness with a defined encounter order, end operations, and a reversed view whose type remains SequencedSet.

Use it for deduplication when first, last, and reverse traversal order are also part of the algorithm.

equals and hashCode retain the Set contract and ignore encounter order, so oppositely ordered SequencedSet instances can be equal.

addFirst and addLast are optional, and a supporting implementation relocates an existing element to the requested end; getFirst throws 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 {
        java.util.SequencedSet<String> set = new java.util.LinkedHashSet<>();
        set.add("a");
        set.add("b");
        System.out.println(set.reversed());
    }
}

Result

[b, a]

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

Sources