Vector定义
public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable
Vector中的操作是线程安全的,Vector对元素的操作方法均有synchronized关键字修饰。
Vector所有API
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 setElementAt(E object, int location)
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()
synchronized void trimToSize()
Vector的数据结构和ArrayList差不多,它包含了3个成员变量elementData , elementCount, capacityIncrement.
(01) elementData 是"Object[]类型的数组",它保存了添加到Vector中的元素。elementData是个动态数组,如果初始化Vector时,没指定动态数组的>大小,则使用默认大小10。随着Vector中元素的增加,Vector的容量也会动态增长,capacityIncrement是与容量增长相关的增长系数,具体的增长方式,请参考源码分析中的ensureCapacity()函数。
(02) elementCount 是动态数组的实际大小。
(03) capacityIncrement 是动态数组的增长系数。如果在创建Vector时,指定了capacityIncrement的大小;则,每次当Vector中动态数组容量增加时,增加的大小都是capacityIncrement。
Vector的构造函数
public Vector() {
this(10);
}
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}
public Vector(Collection<? extends E> c) {
elementData = c.toArray();
elementCount = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
}
Vector四种遍历方式
/**
* FileName: VectorIterator
* Author: Sandy
* Date: 2018/12/11 23:16
* Description: Vector遍历
* Version: v1.0.0
*/
package VectorDemo;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;
public class VectorIterator {
public static void iteratorLinkedListByIterator(Vector vector){
long startTime;
long endTime;
Iterator iterator = vector.iterator();
startTime = System.currentTimeMillis();
while(iterator.hasNext()){
iterator.next();
}
endTime = System.currentTimeMillis();
long interval = endTime - startTime;
System.out.println("通过迭代器遍历Vector效率为:" + interval);
}
public static void iteratorLinkedListByRandomAccess(Vector vector){
long startTime;
long endTime;
Object value;
startTime = System.currentTimeMillis();
for(int i = 0; i < vector.size(); i++){
value = vector.get(i);
}
endTime = System.currentTimeMillis();
long interval = endTime - startTime;
System.out.println("通过随机访问遍历Vector效率为:" + interval);
}
public static void iteratorLinkedListByFor(Vector Vector){
long startTime;
long endTime;
startTime = System.currentTimeMillis();
for(Object object:Vector)
;
endTime = System.currentTimeMillis();
long interval = endTime - startTime;
System.out.println("通过For循环遍历Vector效率为:" + interval);
}
public static void iteratorLinkedListByEnumeration(Vector vector){
long startTime;
long endTime;
startTime = System.currentTimeMillis();
for(Enumeration enu = vector.elements(); enu.hasMoreElements(); ) {
enu.nextElement();
}
endTime = System.currentTimeMillis();
long interval = endTime - startTime;
System.out.println("通过Poll遍历LinkedList效率为:" + interval);
}
public static void main(String[] args) {
Vector vector = new Vector();
for(int i = 0; i < 100000; i++){
vector.add(i);
}
//效率对比
iteratorLinkedListByIterator(vector);
iteratorLinkedListByRandomAccess(vector);
iteratorLinkedListByFor(vector);
iteratorLinkedListByEnumeration(vector);
}
}
输出
通过迭代器遍历Vector效率为:16
通过随机访问遍历Vector效率为:16
通过For循环遍历Vector效率为:16
通过Poll遍历LinkedList效率为:31
Stack
Stack定义
public class Stack<E> extends Vector<E>
Stack是栈。它的特性是:先进后出(FILO, First In Last Out)
Stack常用API
boolean empty()
synchronized E peek()
synchronized E pop()
E push(E object)
synchronized int search(Object o)