Java AbstractList与实例
Java中的 AbstractList 类是Java集合框架的一部分,它实现了集合接口和AbstractCollection类。_ 该类提供了一个List接口的骨架实现,以最大限度地减少实现该接口所需的努力,该接口由一个 随机访问 数据存储(如数组)支持。对于顺序访问数据(如链接列表),应该优先使用AbstractSequentialList而不是这个类。
要实现一个不可修改的列表,只需要扩展这个AbstractList类并实现get(int)和size()方法。实现一个可修改的列表,为此需要额外覆盖set(int index, E element)方法(否则会抛出一个UnsupportedOperationException)。如果列表是可变大小的,我们应该覆盖 add(int, E ) 和 remove(int) 方法。
类的层次结构

声明 。
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>
其中E是这个集合所维护的元素的类型。
构造函数: protected AbstractList() – 默认的构造函数,但是作为保护函数,它不允许创建一个AbstractList对象。
AbstractList<E> al = new ArrayList<E>()
例子1: AbstractList是一个抽象类,所以它应该被分配一个子类的实例,如ArrayList、LinkedList或Vector。
// Java code to illustrate AbstractList
import java.util.*;
public class AbstractListDemo {
public static void main(String args[])
{
// Creating an empty AbstractList
AbstractList<String> list = new ArrayList<String>();
// Use add() method to add elements in the list
list.add("Geeks");
list.add("for");
list.add("Geeks");
list.add("10");
list.add("20");
// Displaying the AbstractList
System.out.println("AbstractList:" + list);
}
}
输出
AbstractList:[Geeks, for, Geeks, 10, 20]
例2 :
// Java code to illustrate
// methods of AbstractCollection
import java.util.*;
public class AbstractListDemo {
public static void main(String args[])
{
// Creating an empty AbstractList
AbstractList<String>
list = new LinkedList<String>();
// Using add() method to add elements in the list
list.add("Geeks");
list.add("for");
list.add("Geeks");
list.add("10");
list.add("20");
// Output the list
System.out.println("AbstractList: " + list);
// Remove the head using remove()
list.remove(3);
// Print the final list
System.out.println("Final AbstractList: " + list);
// getting the index of last occurrence
// using lastIndexOf() method
int lastindex = list.lastIndexOf("A");
// printing the Index
System.out.println("Last index of A : "
+ lastindex);
}
}
输出
AbstractList: [Geeks, for, Geeks, 10, 20]
Final AbstractList: [Geeks, for, Geeks, 20]
Last index of A : -1
抽象列表中的方法
| 方法 | 描述 |
|---|---|
| add(int index, E element) | 在这个列表的指定位置插入指定的元素(可选操作)。 |
| add(E e) | 将指定的元素添加到这个列表的末尾(可选操作)。 |
| addAll(int index, Collection extends E> c) | 将指定集合中的所有元素插入到这个列表的指定位置(可选操作)。 clear() | 从这个列表中移除所有的元素(可选操作)。 equals(Object o) | 将指定的对象与这个列表进行比较,看是否相等。 get(int index) | 返回这个列表中指定位置的元素。 hashCode() | 返回此列表的哈希代码值。 indexOf(Object o) | 返回指定元素在此列表中第一次出现的索引,如果此列表不包含该元素,则返回-1。 iterator() | 以适当的顺序返回此列表中的元素的迭代器。 lastIndexOf(Object o) | 返回指定元素在此列表中最后出现的索引,如果此列表不包含该元素,则返回-1。 listIterator() | 返回这个列表中的元素的迭代器(按适当的顺序)。 listIterator(int index) | 返回这个列表中的元素的列表迭代器(按适当的顺序),从列表中的指定位置开始。 remove(int index) | 移除此列表中指定位置的元素(可选操作)。 removeRange(int fromIndex, int toIndex) | 从这个列表中移除所有索引在fromIndex(包括)和toIndex(不包括)之间的元素。 set(int index, E element) | 用指定的元素替换这个列表中指定位置的元素(可选操作)。 subList(int fromIndex, int toIndex) | 返回此列表中从指定的fromIndex(包括)到toIndex(不包括)之间的部分的视图。 ### java.util.AbstractCollection类中声明的方法 方法 | 描述 |
如果这个集合包含了指定集合中的所有元素,返回真。 |
| isEmpty() | 如果这个集合不包含任何元素,则返回真。 |
| remove(Object o) | 从这个集合中删除指定元素的一个实例,如果它存在的话(可选操作)。 |
| removeAll(Collection> c) | 移除这个集合中所有也包含在指定集合中的元素(可选操作)。 retainAll(Collection> c) |
只保留本集合中包含在指定集合中的元素(可选操作)。 |
| toArray() | 返回一个包含此集合中所有元素的数组。 |
| toArray(T[] a) | 返回一个包含此集合中所有元素的数组;返回的数组的运行时类型是指定数组的类型。 |
| toString() | 返回这个集合的字符串表示。 |
java.util.Collection接口中声明的方法
| 方法 | 描述 |
|---|---|
| parallelStream() | 返回一个以该集合为源的可能的并行流。 |
| removeIf(Predicate super E> filter) | 删除这个集合中满足给定谓词的所有元素。 stream() | 返回一个以这个集合为源的顺序流。 toArray(IntFunction ### java.util.List接口中声明的方法 方法 | 描述 |
如果这个列表包含指定集合的所有元素,返回 true。 |
| isEmpty() | 如果这个列表不包含任何元素,则返回 true。 |
| remove(int index) | 移除这个列表中指定位置的元素(可选操作)。 |
| removeAll(Collection> c) | 从这个列表中移除包含在指定集合中的所有元素(可选操作)。 replaceAll(UnaryOperator retainAll(Collection> c) |
只保留这个列表中包含在指定集合中的元素(可选操作)。 |
| size() | 返回这个列表中元素的数量。 |
| sort(Comparator<? super E> c) | 根据指定的比较器引起的顺序对这个列表进行排序。 |
| spliterator() | 在这个列表中的元素上创建一个Spliterator。 |
| toArray() | 返回一个包含这个列表中所有元素的数组,并以适当的顺序(从第一个元素到最后一个元素)。 |
| toArray(T[] a) | 返回一个包含列表中所有元素的数组(从第一个元素到最后一个元素);返回的数组的运行时类型是指定数组的类型。 |
参考 :https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/AbstractList.html
极客教程