Java中的AbstractList与示例
Java中的AbstractList类是Java Collection Framework 的一部分,实现了Collection 接口和AbstractCollection类。此类提供 List 接口的骨架实现,以最大限度地减少实现此接口所需的工作,该接口由Random Access数据存储(例如数组)支持。对于顺序访问数据(如链表),应优先使用 AbstractSequentialList 而非此类。
要实现一个不可修改的列表,只需要扩展这个 AbstractList 类并实现 get(int) 和 size() 方法。实现一个可修改的列表,为此额外覆盖 set(int index, E element) 方法(否则将引发 UnsupportedOperationException)。如果列表是可变大小的,则应该覆盖 add(int, E) 和 remove(int) 方法。
类层次结构:
宣言:
public abstract class AbstractList extends AbstractCollection implements List
where E is the type of elements maintained by this collection.
构造函数: protected AbstractList() – 默认构造函数,但受到保护,它不允许创建 AbstractList 对象。
AbstractList
示例 1: AbstractList 是一个抽象类,因此应为其分配一个子类的实例,例如 ArrayList、LinkedList 或 Vector。
Java
// Java code to illustrate AbstractList
import java.util.*;
public class AbstractListDemo {
public static void main(String args[])
{
// Creating an empty AbstractList
AbstractList list = new ArrayList();
// 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);
}
}
Java
// Java code to illustrate
// methods of AbstractCollection
import java.util.*;
public class AbstractListDemo {
public static void main(String args[])
{
// Creating an empty AbstractList
AbstractList
list = new LinkedList();
// 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 occurence
// 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]
示例 2:
Java
// Java code to illustrate
// methods of AbstractCollection
import java.util.*;
public class AbstractListDemo {
public static void main(String args[])
{
// Creating an empty AbstractList
AbstractList
list = new LinkedList();
// 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 occurence
// 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
AbstractList 中的方法
METHOD | DESCRIPTION |
---|---|
add(int index, E element) | Inserts the specified element at the specified position in this list (optional operation). |
add(E e) | Appends the specified element to the end of this list (optional operation). |
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). |
clear() | Removes all of the elements from this list (optional operation). |
equals(Object o) | Compares the specified object with this list for equality. |
get(int index) | Returns the element at the specified position in this list. |
hashCode() | Returns the hash code value for this list. |
indexOf(Object o) | Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. |
iterator() | Returns an iterator over the elements in this list in proper sequence. |
lastIndexOf(Object o) | Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. |
listIterator() | Returns a list iterator over the elements in this list (in proper sequence). |
listIterator(int index) | Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. |
remove(int index) | Removes the element at the specified position in this list (optional operation). |
removeRange(int fromIndex, int toIndex) | Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive. |
set(int index, E element) | Replaces the element at the specified position in this list with the specified element (optional operation). |
subList(int fromIndex, int toIndex) | Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. |
在类Java.util.AbstractCollection 中声明的方法
METHOD | DESCRIPTION |
---|---|
addAll(Collection extends E> c) | Adds all of the elements in the specified collection to this collection (optional operation). |
contains(Object o) | Returns true if this collection contains the specified element. |
containsAll(Collection> c) | Returns true if this collection contains all of the elements in the specified collection. |
isEmpty() | Returns true if this collection contains no elements. |
remove(Object o) | Removes a single instance of the specified element from this collection, if it is present (optional operation). |
removeAll(Collection> c) | Removes all of this collection’s elements that are also contained in the specified collection (optional operation). |
retainAll(Collection> c) | Retains only the elements in this collection that are contained in the specified collection (optional operation). |
toArray() | Returns an array containing all of the elements in this collection. |
toArray(T[] a) | Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. |
toString() | Returns a string representation of this collection. |
在接口Java.util.Collection 中声明的方法
METHOD | DESCRIPTION |
---|---|
parallelStream() | Returns a possibly parallel Stream with this collection as its source. |
removeIf(Predicate super E> filter) | Removes all of the elements of this collection that satisfy the given predicate. |
stream() | Returns a sequential Stream with this collection as its source. |
toArray(IntFunction | Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array. |
在接口Java .util.List 中声明的方法
METHOD | DESCRIPTION |
---|---|
addAll(Collection extends E> c) | Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s iterator (optional operation). |
contains(Object o) | Returns true if this list contains the specified element. |
containsAll(Collection> c) | Returns true if this list contains all of the elements of the specified collection. |
isEmpty() | Returns true if this list contains no elements. |
remove(int index) | Removes the element at the specified position in this list (optional operation). |
removeAll(Collection> c) | Removes from this list all of its elements that are contained in the specified collection (optional operation). |
replaceAll(UnaryOperator | Replaces each element of this list with the result of applying the operator to that element. |
retainAll(Collection> c) | Retains only the elements in this list that are contained in the specified collection (optional operation). |
size() | Returns the number of elements in this list. |
sort(Comparator super E> c) | Sorts this list according to the order induced by the specified Comparator. |
spliterator() | Creates a Spliterator over the elements in this list. |
toArray() | Returns an array containing all of the elements in this list in proper sequence (from first to last element). |
toArray(T[] a) | Returns an array containing all of the elements in this list in proper sequence (from first to last element) ; the runtime type of the returned array is that of the specified array. |
参考: Java : Java