Java : AbstractList 示例

Java 中的 AbstractList (Java SE 23 & JDK 23) 及其示例。
您将找到大多数 AbstractList<E> 方法的代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

此类提供 List 接口的骨架实现,以尽量减少实现由“随机访问”数据存储(如数组)支持的此接口所需的工作量。对于顺序访问数据(如链接列表),应优先使用 AbstractSequentialList 而不是此类。 (机器翻译)

Class diagram

class MyList extends AbstractList<String> {
    private final String[] values;

    MyList(String[] values) {
        this.values = Objects.requireNonNull(values);
    }

    @Override
    public String get(int index) {
        return values[index];
    }

    @Override
    public int size() {
        return values.length;
    }
}

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

protected int modCount

此列表被结构修改的次数。 (机器翻译)

class MyList extends AbstractList<String> {
    private final int MAX_SIZE = 5;
    private final String[] values = new String[MAX_SIZE];
    private int size;

    @Override
    public String get(int index) {
        return values[index];
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean add(String s) {
        if (size == MAX_SIZE) {
            throw new IllegalStateException();
        }

        size++;
        modCount++;
        values[size - 1] = s;
        return true;
    }
}

final var list = new MyList();
list.add("aaa");
list.add("bbb");

{
    final var iterator = list.iterator();
    System.out.println("-- iterator --");
    iterator.forEachRemaining(s -> {
        System.out.println(s);
    });

    // Result
    // ↓
    //-- iterator --
    //aaa
    //bbb
}

{
    final var iterator = list.iterator();

    list.add("ccc");

    try {
        System.out.println("-- iterator --");
        iterator.forEachRemaining(s -> {
            System.out.println(s);
        });
    } catch (ConcurrentModificationException e) {
        System.out.println("ConcurrentModificationException!");
    }

    // Result
    // ↓
    //-- iterator --
    //ConcurrentModificationException!
}

Constructors

AbstractList ()

唯一建造者。 (机器翻译)

class MyList extends AbstractList<String> {
    private final String[] values;

    MyList(String[] values) {
        this.values = Objects.requireNonNull(values);
    }

    @Override
    public String get(int index) {
        return values[index];
    }

    @Override
    public int size() {
        return values.length;
    }
}

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)

将指定元素插入此列表的指定位置(可选操作)。 (机器翻译)

final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]

{
    final AbstractList<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    list.add(0, "A");
    System.out.println(list); // [A, a, b, c]
}
{
    final AbstractList<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    list.add(1, "B");
    System.out.println(list); // [a, B, b, c]
}
{
    final AbstractList<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    list.add(2, "C");
    System.out.println(list); // [a, b, C, c]
}
{
    final AbstractList<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    list.add(3, "D");
    System.out.println(list); // [a, b, c, D]
}
{
    final AbstractList<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    //list.add(4, "D"); // IndexOutOfBoundsException
}

boolean add (E e)

将指定元素附加到此列表的末尾(可选操作)。 (机器翻译)

final AbstractList<String> list = new ArrayList<>();
System.out.println(list); // []

System.out.println(list.add("a")); // true
System.out.println(list); // [a]

System.out.println(list.add("b")); // true
System.out.println(list); // [a, b]

System.out.println(list.add("c")); // true
System.out.println(list); // [a, b, c]

boolean addAll (int index, Collection<? extends E> c)

将指定集合中的所有元素插入此列表中的指定位置(可选操作)。 (机器翻译)

final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]

{
    final AbstractList<String> list = new ArrayList<>(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 AbstractList<String> list = new ArrayList<>(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 AbstractList<String> list = new ArrayList<>(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 AbstractList<String> list = new ArrayList<>(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 AbstractList<String> list = new ArrayList<>(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 AbstractList<String> list = new ArrayList<>(src);

System.out.println(list.addAll(3, Set.of("D1", "D2", "D3"))); // true
System.out.println(list); // [a, b, c, D3, D2, D1]

void clear ()

从此列表中删除所有元素(可选操作)。 (机器翻译)

final AbstractList<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");

System.out.println(list); // [a, b, c]

list.clear();
System.out.println(list); // []

boolean equals (Object o)

将指定对象与此列表进行比较以确定其是否相等。 (机器翻译)

final AbstractList<String> list = new ArrayList<>();
Collections.addAll(list, "a", "b", "c");
System.out.println(list); // [a, b, c]

System.out.println(list.equals(List.of("a"))); // false
System.out.println(list.equals(List.of("a", "b"))); // false
System.out.println(list.equals(List.of("a", "b", "c"))); // true
System.out.println(list.equals(List.of("c", "b", "a"))); // false
final AbstractList<String> list = new ArrayList<>();
Collections.addAll(list, "a", "b", "c");
System.out.println(list); // [a, b, c]

final var arrayList = new ArrayList<>(list);
System.out.println(arrayList); // [a, b, c]
System.out.println(list.equals(arrayList)); // true

final var linkedList = new LinkedList<>(list);
System.out.println(linkedList); // [a, b, c]
System.out.println(list.equals(linkedList)); // true
final AbstractList<String> list = new ArrayList<>();
Collections.addAll(list, "a", "b", "c");
System.out.println(list); // [a, b, c]

final var set = new LinkedHashSet<>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]
System.out.println(list.equals(set)); // false

abstract E get (int index)

返回此列表中指定位置的元素。 (机器翻译)

final AbstractList<String> list = new ArrayList<>();
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 AbstractList<Integer> list = new ArrayList<>();
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

int hashCode ()

返回此列表的哈希码值。 (机器翻译)

final AbstractList<String> list1 = new ArrayList<>();
Collections.addAll(list1, "a", "b", "c");
System.out.println(list1.hashCode()); // 126145

final AbstractList<String> list2 = new ArrayList<>();
Collections.addAll(list2, "A", "B", "C");
System.out.println(list2.hashCode()); // 94369

final AbstractList<Integer> list3 = new ArrayList<>();
Collections.addAll(list3, 1, 2, 3);
System.out.println(list3.hashCode()); // 30817

int indexOf (Object o)

返回此列表中第一次出现的指定元素的索引,如果此列表不包含该元素,则返回 -1。 (机器翻译)

final AbstractList<String> list = new ArrayList<>();
Collections.addAll(list, "a", "b", "c", "a", "b", "c");
System.out.println(list); // [a, b, c, a, b, c]

System.out.println(list.indexOf("")); // -1
System.out.println(list.indexOf("a")); // 0
System.out.println(list.indexOf("b")); // 1
System.out.println(list.indexOf("c")); // 2
System.out.println(list.indexOf("d")); // -1
System.out.println(list.indexOf("abc")); // -1

Iterator<E> iterator ()

按正确顺序返回此列表中元素的迭代器。 (机器翻译)

final AbstractList<String> list = new ArrayList<>();
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

int lastIndexOf (Object o)

返回此列表中指定元素最后一次出现的索引,如果此列表不包含该元素,则返回 -1。 (机器翻译)

final AbstractList<String> list = new ArrayList<>();
Collections.addAll(list, "a", "b", "c", "a", "b", "c");
System.out.println(list); // [a, b, c, a, b, c]

System.out.println(list.lastIndexOf("")); // -1
System.out.println(list.lastIndexOf("a")); // 3
System.out.println(list.lastIndexOf("b")); // 4
System.out.println(list.lastIndexOf("c")); // 5
System.out.println(list.lastIndexOf("d")); // -1
System.out.println(list.lastIndexOf("abc")); // -1

ListIterator<E> listIterator ()

返回此列表中元素的列表迭代器(按正确的顺序)。 (机器翻译)

final AbstractList<String> list = new ArrayList<>();
Collections.addAll(list, "aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]

final var listIterator = list.listIterator();
listIterator.forEachRemaining(System.out::println);

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

ListIterator<E> listIterator (int index)

返回此列表中元素的列表迭代器(按正确顺序),从列表中的指定位置开始。 (机器翻译)

final AbstractList<String> list = new ArrayList<>();
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)

删除此列表中指定位置的元素(可选操作)。 (机器翻译)

final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]

{
    final AbstractList<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.remove(0)); // a
    System.out.println(list); // [b, c]
}
{
    final AbstractList<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.remove(1)); // b
    System.out.println(list); // [a, c]
}
{
    final AbstractList<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.remove(2)); // c
    System.out.println(list); // [a, b]
}
{
    final AbstractList<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    //list.remove(3); // IndexOutOfBoundsException
}

protected void removeRange (int fromIndex, int toIndex)

从此列表中删除索引位于 fromIndex(包括)和 toIndex(不包括)之间的所有元素。 (机器翻译)

@SuppressWarnings("serial")
class MyList extends ArrayList<String> {

    MyList(Collection<String> c) {
        super(c);
    }

    @Override
    public void removeRange(int fromIndex, int toIndex) {
        super.removeRange(fromIndex, toIndex);
    }
}

final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]

{
    final var list = new MyList(src);
    System.out.println(list); // [a, b, c]

    list.removeRange(0, 1);
    System.out.println(list); // [b, c]
}
{
    final var list = new MyList(src);
    System.out.println(list); // [a, b, c]

    list.removeRange(0, 2);
    System.out.println(list); // [c]
}
{
    final var list = new MyList(src);
    System.out.println(list); // [a, b, c]

    list.removeRange(0, 3);
    System.out.println(list); // []
}

{
    final var list = new MyList(src);
    System.out.println(list); // [a, b, c]

    list.removeRange(1, 3);
    System.out.println(list); // [a]
}
{
    final var list = new MyList(src);
    System.out.println(list); // [a, b, c]

    list.removeRange(2, 3);
    System.out.println(list); // [a, b]
}
{
    final var list = new MyList(src);
    System.out.println(list); // [a, b, c]

    list.removeRange(3, 3);
    System.out.println(list); // [a, b, c]
}

E set (int index, E element)

用指定元素替换此列表中指定位置的元素(可选操作)。 (机器翻译)

final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]

{
    final AbstractList<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.set(0, "A")); // a
    System.out.println(list); // [A, b, c]
}
{
    final AbstractList<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.set(1, "B")); // b
    System.out.println(list); // [a, B, c]
}
{
    final AbstractList<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.set(2, "C")); // c
    System.out.println(list); // [a, b, C]
}
{
    final AbstractList<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    //list.set(3, "D"); // IndexOutOfBoundsException
}

List<E> subList (int fromIndex, int toIndex)

返回此列表在指定的 fromIndex(包括)和 toIndex(不包括)之间的部分的视图。 (机器翻译)

final AbstractList<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");

System.out.println(list); // [a, b, c, d]

System.out.println(list.subList(0, 0)); // []
System.out.println(list.subList(0, 1)); // [a]
System.out.println(list.subList(0, 2)); // [a, b]
System.out.println(list.subList(0, 3)); // [a, b, c]
System.out.println(list.subList(0, 4)); // [a, b, c, d]
//list.subList(0, 5); // IndexOutOfBoundsException

final var subList = list.subList(1, 3);
System.out.println(subList); // [b, c]
System.out.println(list); // [a, b, c, d]

subList.set(0, "X");
System.out.println(subList); // [X, c]
System.out.println(list); // [a, X, c, d]

subList.add("Y");
System.out.println(subList); // [X, c, Y]
System.out.println(list); // [a, X, c, Y, d]

subList.remove("c");
System.out.println(subList); // [X, Y]
System.out.println(list); // [a, X, Y, d]

Methods declared in AbstractCollection

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

请参阅下面的链接。

Methods declared in Collection

parallelStream, removeIf, stream, toArray

请参阅下面的链接。

Methods declared in Iterable

forEach

请参阅下面的链接。

Methods declared in List

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

请参阅下面的链接。


相关文章

To top of page