List Interface
- List inherits Collection, is an ordered list.
- Implementation class has ArrayList, Vector, LinkedList, Stack, etc.
- ArrayList array is implemented based on, an array queue. You can dynamically add capacity!
- Vector-based arrays to achieve, is a vector queue is thread-safe!
- LinkedList is based on the realization of the list, is a two-way circulation list. It can be used as stack use!
- Stack is based on the array to achieve, is the stack, it inherits the Vector, is characteristic of FILO (last out)
Practical scenario
- if used to queue, stack, linked list, you can use List.
- When the collection of the insertion speed less demanding, but it requires access to data elements of the speed, using the ArrayList
- When set the speed of access to data elements of less demanding, but the insertion and deletion of data for high speed requirements, using the LinkedList
- when the set of multithreaded collection element operating time, this time to be thread safe use Vector! But BVector now generally no longer used.
- When there is a demand in the collection is to preserve the desired data to be read out, using the Stack
ArrayList [focus]
- Array structure to achieve, fast queries, additions and deletions slow;
- JDK1.2 version of the operating efficiency of fast, thread-safe;
ArrayList Introduction
- ArrayList array based implementation, the queue is a dynamic array. But it is not the same and arrays, capacity grows automatically.
- ArrayList inherited AbstractList, realized RandomAccess, Cloneable and Serializable interface
- Integrated AbstractList, AbstractList also inherited the AbstractCollection implements List interface, which is an array queue, provided relevant to add, delete, modify, traversal function
- Implements RandomAccess interface that provides random access, in fact, for quick access by index number.
- Implements the Serializable interface, support for serialization, it means that the sequence of transmission through ArrayList.
Some methods of ArrayList
return value | method |
---|---|
boolean | add(E object) |
boolean | addAll(Collection<? extends E> collection) |
void | clear() |
boolean | contains(Object object) |
boolean | containsAll(Collection<?> collection) |
boolean | equals(Object object) |
int | hashCode() |
boolean | isEmpty() |
Iterator | iterator() |
boolean | remove(Object object) |
boolean | removeAll(Collection<?> collection) |
Object | clone() |
List | subList(int start, int end) |
E | set(int location, E object) |
E | remove(int location) |
ListIterator | listIterator () |
ListIterator | listIterator(int location) |
int | lastIndexOf(Object object) |
int | indexOf(Object object) |
E | get(int location) |
boolean | addAll(int location, Collection<? extends E> collection) |
void | add(int location, E object) |
Object[] | toArray() |
T[] | toArray(T[] array) |
int | size() |
boolean | retainAll(Collection<?> collection) |
void | trimToSize() |
void | removeRange(int fromIndex, int toIndex) |
void | ensureCapacity(int minimumCapacity) |
Vector
- Array structure to achieve, fast queries, additions and deletions slow;
- JDK1.0 version, operating efficiency is slow, thread-safe;
- Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index.
- Vector inherited AbstractList, to achieve the List; therefore, it is a queue, support-related add, delete, modify, traverse functions.
- vector achieve a RandmoAccess interface, that provides random access function. RandmoAccess is being used in java implementation of List, provides quick access to the List. In Vector, we can quickly get that is through a number of elements of the element object; this is the fast random access.
- Vector implement the Serializable interface, support for serialization.
synchronized boolean add(E object)
void add(int location, E object)
synchronized boolean addAll(Collection<? extends E> collection)
synchronized boolean addAll(int location, Collection<? extends E> collection)
synchronized void addElement(E object)
synchronized int capacity()
void clear()
synchronized Object clone()
boolean contains(Object object)
synchronized boolean containsAll(Collection<?> collection)
synchronized void copyInto(Object[] elements)
synchronized E elementAt(int location)
Enumeration<E> elements()
synchronized void ensureCapacity(int minimumCapacity)
synchronized boolean equals(Object object)
synchronized E firstElement()
E get(int location)
synchronized int hashCode()
synchronized int indexOf(Object object, int location)
int indexOf(Object object)
synchronized void insertElementAt(E object, int location)
synchronized boolean isEmpty()
synchronized E lastElement()
synchronized int lastIndexOf(Object object, int location)
synchronized int lastIndexOf(Object object)
synchronized E remove(int location)
boolean remove(Object object)
synchronized boolean removeAll(Collection<?> collection)
synchronized void removeAllElements()
synchronized boolean removeElement(Object object)
synchronized void removeElementAt(int location)
synchronized boolean retainAll(Collection<?> collection)
synchronized E set(int location, E object)
synchronized void setSize(int length)
synchronized int size()
synchronized List<E> subList(int start, int end)
synchronized <T> T[] toArray(T[] contents)
synchronized Object[] toArray()
synchronized String toString()
- [Note]: Vector with basically the same knowledge ArrayList Vector is thread-safe multi-synchronize modified, so the speed is much slower than ArrayList
LinkedList
- Array structure to achieve, slow queries, additions and deletions fast;
- JDK1.2 version of the operating efficiency of fast, thread-safe;
- LinkedList is implemented based on the list, it can be seen from the source is a doubly linked list. In addition to use as a linked list, but it can also be used as a stack, queue, or deque operate. Is thread safe.
- LinkedList implement Deque interfaces, i.e., it can be deemed LinkedList deque use.
- LinkedList implement the java.io.Serializable interface, which means LinkedList support serialization, go through the sequence of transmission.
boolean add(E e)
将指定元素添加到此列表的结尾。
void add(int index, E element)
在此列表中指定的位置插入指定的元素。
boolean addAll(Collection<? extends E> c)
添加指定 collection 中的所有元素到此列表的结尾,顺序是指定 collection 的迭代器返回这些元素的顺序。
boolean addAll(int index, Collection<? extends E> c)
将指定 collection 中的所有元素从指定位置开始插入此列表。
void addFirst(E e)
将指定元素插入此列表的开头。
void addLast(E e)
将指定元素添加到此列表的结尾。
void clear()
从此列表中移除所有元素。
Object clone()
返回此 LinkedList 的浅表副本。
boolean contains(Object o)
如果此列表包含指定元素,则返回 true。
Iterator<E> descendingIterator()
返回以逆向顺序在此双端队列的元素上进行迭代的迭代器。
E element()
获取但不移除此列表的头(第一个元素)。
E get(int index)
返回此列表中指定位置处的元素。
E getFirst()
返回此列表的第一个元素。
E getLast()
返回此列表的最后一个元素。
int indexOf(Object o)
返回此列表中首次出现的指定元素的索引,如果此列表中不包含该元素,则返回 -1。
int lastIndexOf(Object o)
返回此列表中最后出现的指定元素的索引,如果此列表中不包含该元素,则返回 -1。
ListIterator<E> listIterator(int index)
返回此列表中的元素的列表迭代器(按适当顺序),从列表中指定位置开始。
boolean offer(E e)
将指定元素添加到此列表的末尾(最后一个元素)。
boolean offerFirst(E e)
在此列表的开头插入指定的元素。
boolean offerLast(E e)
在此列表末尾插入指定的元素。
E peek()
获取但不移除此列表的头(第一个元素)。
E peekFirst()
获取但不移除此列表的第一个元素;如果此列表为空,则返回 null。
E peekLast()
获取但不移除此列表的最后一个元素;如果此列表为空,则返回 null。
E poll()
获取并移除此列表的头(第一个元素)
E pollFirst()
获取并移除此列表的第一个元素;如果此列表为空,则返回 null。
E pollLast()
获取并移除此列表的最后一个元素;如果此列表为空,则返回 null。
E pop()
从此列表所表示的堆栈处弹出一个元素。
void push(E e)
将元素推入此列表所表示的堆栈。
E remove()
获取并移除此列表的头(第一个元素)。
E remove(int index)
移除此列表中指定位置处的元素。
boolean remove(Object o)
从此列表中移除首次出现的指定元素(如果存在)。
E removeFirst()
移除并返回此列表的第一个元素。
boolean removeFirstOccurrence(Object o)
从此列表中移除第一次出现的指定元素(从头部到尾部遍历列表时)。
E removeLast()
移除并返回此列表的最后一个元素。
boolean removeLastOccurrence(Object o)
从此列表中移除最后一次出现的指定元素(从头部到尾部遍历列表时)。
E set(int index, E element)
将此列表中指定位置的元素替换为指定的元素。
int size()
返回此列表的元素数。
Object[] toArray()
返回以适当顺序(从第一个元素到最后一个元素)包含此列表中所有元素的数组。
<T> T[] toArray(T[] a)
返回以适当顺序(从第一个元素到最后一个元素)包含此列表中所有元素的数组;返回数组的运行时类型为指定数组的类型。
- LinkedList is based on the realization of two-way circular linked list, realize the List and Deque interfaces. Implements all optional list operations, and permits all elements.
- LinkedList not thread-safe, suitable only for use in single-threaded.
- and listIterator iterator iterator class method returns the fail-fast, to pay attention ConcurrentModificationException.
- LinkedList implements Serializable interface, so it supports serialized, serialization can be transmitted by achieving a Cloneable interface, be cloned.
- When find and delete an element, is divided into the element is null and not null two cases to deal with, LinkedList allowed element is null.
- Because it is based on the list, there is no expansion method LinkedList, the default added element is the tail of automatic expansion.
- LinkedList also achieved queue stack and method of operation, and therefore may be used as a stack, and queue deque used, such as peek, push, pop or the like.
- LinkedList is implemented based on the list, and therefore high efficiency insert delete, search efficiency is very low (
Stack
- Stack class represents a last in first out (LIFO) stack of objects.
- Push and pop operation and to provide a method peek, empty method, search methods.
- Inherited from Vector, it is the realization of the principle of the array implementation of a stack.
- Thread-safe
boolean empty()
测试堆栈是否为空。
E peek()
查看堆栈顶部的对象,但不从堆栈中移除它。
E pop()
移除堆栈顶部的对象,并作为此函数的值返回该对象。
E push(E item)
把项压入堆栈顶部。
int search(Object o)
返回对象在堆栈中的位置,以 1 为基数。
-
Stack is actually to achieve through the array. The actual implementation is a method called Vector in!
-
push (i.e., the element onto the stack), the element is added by the end of the array.
-
When peek (i.e., remove the top element, not delete), return end of the array elements.
-
pop (i.e., remove the top element, and the element from the stack), the end of the array elements is removed, and then remove the element from the array.