Java : AbstractSequentialList con ejemplos

AbstractSequentialList (Java SE 23 & JDK 23) en Java con ejemplos.
Encontrará muestras de código para la mayoría de los métodos AbstractSequentialList<E>.

Nota :


Summary

Esta clase proporciona una implementación esquelética de la interfaz List para minimizar el esfuerzo requerido para implementar esta interfaz respaldada por un almacén de datos de "acceso secuencial" (como una lista enlazada). (Traducción automática)

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

Consulte el siguiente enlace.

Constructors

AbstractSequentialList ()

Constructor único. (Traducción automática)

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)

Inserta el elemento especificado en la posición especificada en esta lista (operación opcional). (Traducción automática)

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)

Inserta todos los elementos de la colección especificada en esta lista en la posición especificada (operación opcional). (Traducción automática)

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)

Devuelve el elemento en la posición especificada en esta lista. (Traducción automática)

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

Devuelve un iterador sobre los elementos de esta lista (en la secuencia adecuada). (Traducción automática)

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)

Devuelve un iterador de lista sobre los elementos de esta lista (en la secuencia adecuada). (Traducción automática)

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)

Elimina el elemento en la posición especificada en esta lista (operación opcional). (Traducción automática)

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)

Reemplaza el elemento en la posición especificada en esta lista con el elemento especificado (operación opcional). (Traducción automática)

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

Consulte el siguiente enlace.

Methods declared in AbstractCollection

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

Consulte el siguiente enlace.

Methods declared in Collection

parallelStream, removeIf, stream, toArray

Consulte el siguiente enlace.

Methods declared in Iterable

forEach

Consulte el siguiente enlace.

Methods declared in List

addAll, addFirst, addLast, contains, containsAll, getFirst, getLast, isEmpty, remove, removeAll, removeFirst, removeLast, replaceAll, retainAll, reversed, size, sort, spliterator, toArray, toArray

Consulte el siguiente enlace.


Related posts

To top of page