Java : SequencedCollection with Examples
SequencedCollection (Java SE 23 & JDK 23) in Java with Examples.
You will find code samples for most of the SequencedCollection<E> methods.
Summary
A collection that has a well-defined encounter order, that supports operations at both ends, and that is reversible. The elements of a sequenced collection have an encounter order, where conceptually the elements have a linear arrangement from the first element to the last element.
final SequencedCollection<String> sc = new ArrayList<>();
sc.addLast("a");
sc.addLast("b");
sc.addLast("c");
System.out.println(sc); // [a, b, c]
System.out.println(sc.reversed()); // [c, b, a]
sc.addFirst("X");
sc.addFirst("Y");
sc.addFirst("Z");
System.out.println(sc); // [Z, Y, X, a, b, c]
System.out.println(sc.reversed()); // [c, b, a, X, Y, Z]
Methods
default void addFirst (E e)
Adds an element as the first element of this collection (optional operation).
final SequencedCollection<String> sc = new ArrayList<>();
sc.addFirst("aaa");
System.out.println(sc); // [aaa]
sc.addFirst("bbb");
System.out.println(sc); // [bbb, aaa]
sc.addFirst("ccc");
System.out.println(sc); // [ccc, bbb, aaa]
default void addLast (E e)
Adds an element as the last element of this collection (optional operation).
final SequencedCollection<String> sc = new ArrayList<>();
sc.addLast("aaa");
System.out.println(sc); // [aaa]
sc.addLast("bbb");
System.out.println(sc); // [aaa, bbb]
sc.addLast("ccc");
System.out.println(sc); // [aaa, bbb, ccc]
default E getFirst ()
Gets the first element of this collection.
final SequencedCollection<String> sc = new ArrayList<>();
sc.addLast("aaa");
System.out.println(sc); // [aaa]
System.out.println(sc.getFirst()); // aaa
sc.addLast("bbb");
System.out.println(sc); // [aaa, bbb]
System.out.println(sc.getFirst()); // aaa
sc.addLast("ccc");
System.out.println(sc); // [aaa, bbb, ccc]
System.out.println(sc.getFirst()); // aaa
final SequencedCollection<String> sc = new ArrayList<>();
System.out.println(sc); // []
try {
var _ = sc.getFirst();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// Result
// ↓
//NoSuchElementException!
default E getLast ()
Gets the last element of this collection.
final SequencedCollection<String> sc = new ArrayList<>();
sc.addLast("aaa");
System.out.println(sc); // [aaa]
System.out.println(sc.getLast()); // aaa
sc.addLast("bbb");
System.out.println(sc); // [aaa, bbb]
System.out.println(sc.getLast()); // bbb
sc.addLast("ccc");
System.out.println(sc); // [aaa, bbb, ccc]
System.out.println(sc.getLast()); // ccc
final SequencedCollection<String> sc = new ArrayList<>();
System.out.println(sc); // []
try {
var _ = sc.getLast();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// Result
// ↓
//NoSuchElementException!
default E removeFirst ()
Removes and returns the first element of this collection (optional operation).
final SequencedCollection<String> sc = new ArrayList<>();
sc.addLast("aaa");
sc.addLast("bbb");
sc.addLast("ccc");
System.out.println(sc); // [aaa, bbb, ccc]
System.out.println(sc.removeFirst()); // aaa
System.out.println(sc); // [bbb, ccc]
System.out.println(sc.removeFirst()); // bbb
System.out.println(sc); // [ccc]
System.out.println(sc.removeFirst()); // ccc
System.out.println(sc); // []
final SequencedCollection<String> sc = new ArrayList<>();
System.out.println(sc); // []
try {
var _ = sc.removeFirst();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// Result
// ↓
//NoSuchElementException!
default E removeLast ()
Removes and returns the last element of this collection (optional operation).
final SequencedCollection<String> sc = new ArrayList<>();
sc.addLast("aaa");
sc.addLast("bbb");
sc.addLast("ccc");
System.out.println(sc); // [aaa, bbb, ccc]
System.out.println(sc.removeLast()); // ccc
System.out.println(sc); // [aaa, bbb]
System.out.println(sc.removeLast()); // bbb
System.out.println(sc); // [aaa]
System.out.println(sc.removeLast()); // aaa
System.out.println(sc); // []
final SequencedCollection<String> sc = new ArrayList<>();
System.out.println(sc); // []
try {
var _ = sc.removeLast();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// Result
// ↓
//NoSuchElementException!
SequencedCollection<E> reversed ()
Returns a reverse-ordered view of this collection.
final SequencedCollection<String> sc = new ArrayList<>();
sc.addLast("aaa");
sc.addLast("bbb");
sc.addLast("ccc");
System.out.println(sc); // [aaa, bbb, ccc]
final var reversedSc = sc.reversed();
System.out.println(reversedSc); // [ccc, bbb, aaa]
System.out.println(reversedSc.reversed()); // [aaa, bbb, ccc]
Methods declared in Collection
add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, parallelStream, remove, removeAll, removeIf, retainAll, size, spliterator, stream, toArray, toArray, toArray
Please see the link below.
Methods declared in Iterable
Related posts
- API Examples