Java : SequencedSet with Examples

SequencedSet (Java SE 21 & JDK 21) with Examples.
You will find code examples on most SequencedSet methods.


Summary

A collection that is both a SequencedCollection and a Set. As such, it can be thought of either as a Set that also has a well-defined encounter order, or as a SequencedCollection that also has unique elements.

Class diagram

final SequencedSet<String> set = new LinkedHashSet<>();

set.addLast("a");
set.addLast("b");
set.addLast("c");

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

set.addFirst("X");
set.addFirst("Y");
set.addFirst("Z");

System.out.println(set); // [Z, Y, X, a, b, c]
System.out.println(set.reversed()); // [c, b, a, X, Y, Z]

Methods

SequencedSet<E> reversed ()

Returns a reverse-ordered view of this collection.

final SequencedSet<String> set = new LinkedHashSet<>();

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

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

final var reversedSet = set.reversed();
System.out.println(reversedSet); // [ccc, bbb, aaa]

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

Methods declared in Collection

parallelStream, removeIf, stream, toArray

Please see the link below.

Methods declared in Iterable

forEach

Please see the link below.

Methods declared in SequencedCollection

addFirst, addLast, getFirst, getLast, removeFirst, removeLast

Please see the link below.

Methods declared in Set

add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, spliterator, toArray, toArray

Please see the link below.


Related posts

To top of page