Java : AbstractSequentialList - API使用例
AbstractSequentialList (Java SE 18 & JDK 18) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
必要最低限の実装例です。
class MyList extends AbstractSequentialList<String> {
private final String[] values;
MyList(String[] values) {
this.values = Objects.requireNonNull(values);
}
@Override
public ListIterator<String> listIterator(int index) {
return new ListIteratorImpl(index);
}
@Override
public int size() {
return values.length;
}
private class ListIteratorImpl implements ListIterator<String> {
private int cursor;
private ListIteratorImpl(int index) {
if (index < 0 || index > size()) {
throw new IndexOutOfBoundsException();
}
this.cursor = index;
}
@Override
public boolean hasNext() {
return size() != cursor;
}
@Override
public String next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
final var value = values[cursor];
cursor++;
return value;
}
@Override
public boolean hasPrevious() {
return 0 != cursor;
}
@Override
public String previous() {
if (!hasPrevious()) {
throw new NoSuchElementException();
}
cursor--;
return values[cursor];
}
@Override
public int nextIndex() {
return cursor;
}
@Override
public int previousIndex() {
return cursor - 1;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@Override
public void set(String s) {
throw new UnsupportedOperationException();
}
@Override
public void add(String s) {
throw new UnsupportedOperationException();
}
}
}
final var list = new MyList(new String[]{"aaa", "bbb", "ccc"});
System.out.println(list); // [aaa, bbb, ccc]
System.out.println(list.size()); // 3
System.out.println(list.isEmpty()); // false
System.out.println("-- values --");
for (final var value : list) {
System.out.println(value);
}
// 結果
// ↓
//-- values --
//aaa
//bbb
//ccc
関連記事:List(リスト)の基本
AbstractListで宣言されたフィールド
modCount
「Java API 使用例 : AbstractList」をご参照ください。
コンストラクタ
AbstractSequentialList ()
class MyList extends AbstractSequentialList<String> {
private final String[] values;
MyList(String[] values) {
this.values = Objects.requireNonNull(values);
}
@Override
public ListIterator<String> listIterator(int index) {
return new ListIteratorImpl(index);
}
@Override
public int size() {
return values.length;
}
private class ListIteratorImpl implements ListIterator<String> {
private int cursor;
private ListIteratorImpl(int index) {
if (index < 0 || index > size()) {
throw new IndexOutOfBoundsException();
}
this.cursor = index;
}
@Override
public boolean hasNext() {
return size() != cursor;
}
@Override
public String next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
final var value = values[cursor];
cursor++;
return value;
}
@Override
public boolean hasPrevious() {
return 0 != cursor;
}
@Override
public String previous() {
if (!hasPrevious()) {
throw new NoSuchElementException();
}
cursor--;
return values[cursor];
}
@Override
public int nextIndex() {
return cursor;
}
@Override
public int previousIndex() {
return cursor - 1;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@Override
public void set(String s) {
throw new UnsupportedOperationException();
}
@Override
public void add(String s) {
throw new UnsupportedOperationException();
}
}
}
final var list = new MyList(new String[]{"aaa", "bbb", "ccc"});
System.out.println(list); // [aaa, bbb, ccc]
System.out.println(list.size()); // 3
System.out.println(list.isEmpty()); // false
System.out.println("-- values --");
for (final var value : list) {
System.out.println(value);
}
// 結果
// ↓
//-- values --
//aaa
//bbb
//ccc
メソッド
void add (int index, E element)
final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]
{
final AbstractSequentialList<String> list = new LinkedList<>(src);
System.out.println(list); // [a, b, c]
list.add(0, "A");
System.out.println(list); // [A, a, b, c]
}
{
final AbstractSequentialList<String> list = new LinkedList<>(src);
System.out.println(list); // [a, b, c]
list.add(1, "B");
System.out.println(list); // [a, B, b, c]
}
{
final AbstractSequentialList<String> list = new LinkedList<>(src);
System.out.println(list); // [a, b, c]
list.add(2, "C");
System.out.println(list); // [a, b, C, c]
}
{
final AbstractSequentialList<String> list = new LinkedList<>(src);
System.out.println(list); // [a, b, c]
list.add(3, "D");
System.out.println(list); // [a, b, c, D]
}
{
final AbstractSequentialList<String> list = new LinkedList<>(src);
System.out.println(list); // [a, b, c]
//list.add(4, "D"); // IndexOutOfBoundsException
}
boolean addAll (int index, Collection<? extends E> c)
final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]
{
final AbstractSequentialList<String> list = new LinkedList<>(src);
System.out.println(list); // [a, b, c]
System.out.println(list.addAll(0, List.of("A1", "A2"))); // true
System.out.println(list); // [A1, A2, a, b, c]
}
{
final AbstractSequentialList<String> list = new LinkedList<>(src);
System.out.println(list); // [a, b, c]
System.out.println(list.addAll(1, List.of("B1", "B2", "B3"))); // true
System.out.println(list); // [a, B1, B2, B3, b, c]
}
{
final AbstractSequentialList<String> list = new LinkedList<>(src);
System.out.println(list); // [a, b, c]
System.out.println(list.addAll(2, List.of("C1", "C2", "C3", "C4"))); // true
System.out.println(list); // [a, b, C1, C2, C3, C4, c]
}
{
final AbstractSequentialList<String> list = new LinkedList<>(src);
System.out.println(list); // [a, b, c]
System.out.println(list.addAll(3, List.of())); // false
System.out.println(list); // [a, b, c]
}
{
final AbstractSequentialList<String> list = new LinkedList<>(src);
System.out.println(list); // [a, b, c]
//list.addAll(4, List.of("E")); // IndexOutOfBoundsException
}
final var src = List.of("a", "b", "c");
System.out.println(src);// [a, b, c]
final AbstractSequentialList<String> list = new LinkedList<>(src);
// Setは順序が保証されません。
System.out.println(list.addAll(3, Set.of("D1", "D2", "D3"))); // true
System.out.println(list); // [a, b, c, D3, D2, D1]
E get (int index)
final AbstractSequentialList<String> list = new LinkedList<>();
Collections.addAll(list, "a", "b", "c");
System.out.println(list); // [a, b, c]
//list.get(-1); // IndexOutOfBoundsException
System.out.println(list.get(0)); // a
System.out.println(list.get(1)); // b
System.out.println(list.get(2)); // c
//list.get(3); // IndexOutOfBoundsException
final AbstractSequentialList<Integer> list = new LinkedList<>();
Collections.addAll(list, 10, 20, 30, 40);
System.out.println(list); // [10, 20, 30, 40]
System.out.println(list.get(0)); // 10
System.out.println(list.get(1)); // 20
System.out.println(list.get(2)); // 30
System.out.println(list.get(3)); // 40
Iterator<E> iterator ()
final AbstractSequentialList<String> list = new LinkedList<>();
Collections.addAll(list, "aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]
final var iterator = list.iterator();
iterator.forEachRemaining(System.out::println);
// 結果
// ↓
//aaa
//bbb
//ccc
abstract ListIterator<E> listIterator (int index)
final AbstractSequentialList<String> list = new LinkedList<>();
Collections.addAll(list, "aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]
{
final var ret = list.listIterator(0);
ret.forEachRemaining(System.out::println);
// 結果
// ↓
//aaa
//bbb
//ccc
}
{
final var ret = list.listIterator(1);
ret.forEachRemaining(System.out::println);
// 結果
// ↓
//bbb
//ccc
}
{
final var ret = list.listIterator(2);
ret.forEachRemaining(System.out::println);
// 結果
// ↓
//ccc
}
E remove (int index)
final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]
{
final AbstractSequentialList<String> list = new LinkedList<>(src);
System.out.println(list); // [a, b, c]
System.out.println(list.remove(0)); // a
System.out.println(list); // [b, c]
}
{
final AbstractSequentialList<String> list = new LinkedList<>(src);
System.out.println(list); // [a, b, c]
System.out.println(list.remove(1)); // b
System.out.println(list); // [a, c]
}
{
final AbstractSequentialList<String> list = new LinkedList<>(src);
System.out.println(list); // [a, b, c]
System.out.println(list.remove(2)); // c
System.out.println(list); // [a, b]
}
{
final AbstractSequentialList<String> list = new LinkedList<>(src);
System.out.println(list); // [a, b, c]
//list.remove(3); // IndexOutOfBoundsException
}
E set (int index, E element)
final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]
{
final AbstractSequentialList<String> list = new LinkedList<>(src);
System.out.println(list); // [a, b, c]
System.out.println(list.set(0, "A")); // a
System.out.println(list); // [A, b, c]
}
{
final AbstractSequentialList<String> list = new LinkedList<>(src);
System.out.println(list); // [a, b, c]
System.out.println(list.set(1, "B")); // b
System.out.println(list); // [a, B, c]
}
{
final AbstractSequentialList<String> list = new LinkedList<>(src);
System.out.println(list); // [a, b, c]
System.out.println(list.set(2, "C")); // c
System.out.println(list); // [a, b, C]
}
{
final AbstractSequentialList<String> list = new LinkedList<>(src);
System.out.println(list); // [a, b, c]
//list.set(3, "D"); // IndexOutOfBoundsException
}
AbstractListで宣言されたメソッド
add, clear, equals, hashCode, indexOf, lastIndexOf, listIterator, removeRange, subList
「Java API 使用例 : AbstractList」をご参照ください。
AbstractCollectionで宣言されたメソッド
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
「Java API 使用例 : AbstractCollection」をご参照ください。
Collectionで宣言されたメソッド
parallelStream, removeIf, stream, toArray
「Java API 使用例 : Collection」をご参照ください。
Iterableで宣言されたメソッド
forEach
「Java API 使用例 : Iterable」をご参照ください。
Listで宣言されたメソッド
addAll, contains, containsAll, isEmpty, remove, removeAll, replaceAll, retainAll, size, sort, spliterator, toArray, toArray
「Java API 使用例 : List」をご参照ください。
関連記事
- List(リスト)の基本
- Map(マップ)の基本
- Set(セット)の基本
- List の初期化方法いろいろ
- Map の初期化方法いろいろ
- Set の初期化方法いろいろ
- 配列 vs. List
- 配列からListへの変換
- List から配列への変換
- API 使用例
- Collection (コレクション)
- Collections (コレクション操作)
- Comparable
- Comparator
- Iterator
- List (リスト)
- Map (マップ)
- Map.Entry (キーと値のペア)
- Queue (キュー)
- AbstractQueue
- ArrayBlockingQueue (ブロッキング・配列キュー)
- ArrayDeque (両端キュー)
- BlockingDeque (ブロッキング・両端キュー)
- BlockingQueue (ブロッキング・キュー)
- ConcurrentLinkedDeque (並列処理用・両端キュー)
- ConcurrentLinkedQueue (並列処理用キュー)
- Deque (両端キュー)
- LinkedBlockingDeque (ブロッキング・リンク両端キュー)
- LinkedBlockingQueue (ブロッキング・リンクキュー)
- LinkedList (二重リンク・リスト)
- PriorityBlockingQueue (ブロッキング・優先度付きキュー)
- PriorityQueue (優先度付きキュー)
- RandomAccess
- Set (セット)
- Spliterator