Java : AbstractSequentialList with Examples

AbstractSequentialList (Java SE 18 & JDK 18) API Examples.
You will find code examples on most AbstractSequentialList methods.


Summary

This class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a "sequential access" data store (such as a linked list).

Class diagram

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);
}

// Result
// ↓
//-- values --
//aaa
//bbb
//ccc

Fields declared in AbstractList

modCount

Please see the link below.

Constructors

AbstractSequentialList ()

Sole constructor.

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);
}

// Result
// ↓
//-- values --
//aaa
//bbb
//ccc

Methods

void add (int index, E element)

Inserts the specified element at the specified position in this list (optional operation).

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)

Inserts all of the elements in the specified collection into this list at the specified position (optional operation).

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);

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)

Returns the element at the specified position in this list.

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 ()

Returns an iterator over the elements in this list (in proper sequence).

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);

// Result
// ↓
//aaa
//bbb
//ccc

abstract ListIterator<E> listIterator (int index)

Returns a list iterator over the elements in this list (in proper sequence).

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);

    // Result
    // ↓
    //aaa
    //bbb
    //ccc
}
{
    final var ret = list.listIterator(1);
    ret.forEachRemaining(System.out::println);

    // Result
    // ↓
    //bbb
    //ccc
}
{
    final var ret = list.listIterator(2);
    ret.forEachRemaining(System.out::println);

    // Result
    // ↓
    //ccc
}

E remove (int index)

Removes the element at the specified position in this list (optional operation).

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)

Replaces the element at the specified position in this list with the specified element (optional operation).

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
}

Methods declared in AbstractList

add, clear, equals, hashCode, indexOf, lastIndexOf, listIterator, removeRange, subList

Please see the link below.

Methods declared in AbstractCollection

addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString

Please see the link below.

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 List

addAll, contains, containsAll, isEmpty, remove, removeAll, replaceAll, retainAll, size, sort, spliterator, toArray, toArray

Please see the link below.


Related posts

To top of page