Java : SequencedCollection with Examples
SequencedCollection (Java SE 21 & JDK 21) with Examples.
You will find code examples on most SequencedCollection methods.
Summary
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)
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)
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 ()
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 {
final var ret = sc.getFirst();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// Result
// ↓
//NoSuchElementException!
default E getLast ()
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 {
final var ret = sc.getLast();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// Result
// ↓
//NoSuchElementException!
default E removeFirst ()
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 {
final var ret = sc.removeFirst();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// Result
// ↓
//NoSuchElementException!
default E removeLast ()
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 {
final var ret = sc.removeLast();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// Result
// ↓
//NoSuchElementException!
SequencedCollection<E> reversed ()
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