Java : SequencedCollection 示例
Java 中的 SequencedCollection (Java SE 23 & JDK 23) 及其示例。
您将找到大多数 SequencedCollection<E> 方法的代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
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 {
var _ = 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 {
var _ = 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 {
var _ = 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 {
var _ = 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
请参阅下面的链接。