【JAVA】五 JAVA集合 Collection Vector Stack
2016-05-29 08:53
555 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/maguochao_Mark/article/details/51493983
【JAVA】五 JAVA集合 Collection Vector Stack
Collection
Collection 已知子集
BeanContext, BeanContextServices, BlockingDeque<E>, BlockingQueue<E>, Deque<E>, List<E>,NavigableSet<E>, Queue<E>, Set<E>, SortedSet<E>, TransferQueue<E>
Vector
Vector 与 ArrayList LinkedList 同属于List子类 .
Vector线程安全的动态数组
Vector属性
public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable { /** * The array buffer into which the components of the vector are * stored. The capacity of the vector is the length of this array buffer, * and is at least large enough to contain all the vector's elements. * * <p>Any array elements following the last element in the Vector are null. * * @serial */ protected Object[] elementData; /** * The number of valid components in this {@code Vector} object. * Components {@code elementData[0]} through * {@code elementData[elementCount-1]} are the actual items. * * @serial */ protected int elementCount; /** * The amount by which the capacity of the vector is automatically * incremented when its size becomes greater than its capacity. If * the capacity increment is less than or equal to zero, the capacity * of the vector is doubled each time it needs to grow. * * @serial */ protected int capacityIncrement; /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = -2767605614048989439L; }
Vector构造方法
/** * 构造一个指定初始容量和增加容量的vector。 * * @param initialCapacity the initial capacity of the vector * @param capacityIncrement the amount by which the capacity is * increased when the vector overflows * @throws IllegalArgumentException if the specified initial capacity * is negative */ public Vector(int initialCapacity, int capacityIncrement) { super(); if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); this.elementData = new Object[initialCapacity]; this.capacityIncrement = capacityIncrement; } /** * 构造一个指定初始容量的vector * 以其容量增量为 0 。 * @param initialCapacity the initial capacity of the vector * @throws IllegalArgumentException if the specified initial capacity * is negative */ public Vector(int initialCapacity) { this(initialCapacity, 0); } /** * 构造一个空vector * 其内部数据数组10个空间 * 及其容量增加为 0 。 */ public Vector() { this(10); } /** * 接收一个Collection的子集,将子集转换为vector类型 * * @param c the collection whose elements are to be placed into this * vector * @throws NullPointerException if the specified collection is null * @since 1.2 */ 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 线程安全
Vector简单理解为线程安全的ArrayList , 通过代码来看看Vector是如何实现线程安全的.
public synchronized void trimToSize() public synchronized void ensureCapacity(int minCapacity) public synchronized int indexOf(Object o, int index) public synchronized int lastIndexOf(Object o) public synchronized void removeElementAt(int index) public synchronized E get(int index) public synchronized E set(int index, E element) ...
这里没有贴出方法的实现, 其实方法实现和ArrayList 一样 , Vector只是在声明方法时加入了synchronized关键字 .
Stack
Stack:后进先出,实现了一些栈基本操作的方法
(其实并不是只能后进先出,因为继承自Vector就会有很多父类的方法,从某种意义上来讲,不是一个栈)
public class Stack<E> extends Vector<E> { /** * Creates an empty Stack. */ public Stack() { } /** * 把一个项目添加到堆栈的顶部。 * * @param item the item to be pushed onto this stack. * @return the <code>item</code> argument. * @see java.util.Vector#addElement */ public E push(E item) { addElement(item); return item; } /** * 删除这个堆栈的顶部的对象并返回对象。 * * @return The object at the top of this stack (the last item * of the <tt>Vector</tt> object). * @throws EmptyStackException if this stack is empty. */ public synchronized E pop() { E obj; int len = size(); obj = peek(); removeElementAt(len - 1); return obj; } /** * 查看这个堆栈的顶部的对象。 * * @return the object at the top of this stack (the last item * of the <tt>Vector</tt> object). * @throws EmptyStackException if this stack is empty. */ public synchronized E peek() { int len = size(); if (len == 0) throw new EmptyStackException(); return elementAt(len - 1); } /** * Tests if this stack is empty. * * @return <code>true</code> if and only if this stack contains * no items; <code>false</code> otherwise. */ public boolean empty() { return size() == 0; } /** * * 最上面查找对象 * 找到返回尾部位置 * 反之 -1 * * @param o the desired object. * @return the 1-based position from the top of the stack where * the object is located; the return value <code>-1</code> * indicates that the object is not on the stack. */ public synchronized int search(Object o) { int i = lastIndexOf(o); if (i >= 0) { return size() - i; } return -1; } /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = 1224463164541339165L; }
相关文章推荐
- Java集合之Vector、Stack
- Java 7之集合类型 - Vector与Stack
- Java 集合:Collection,List,ArrayList,Vector,LinkedList(实现方式,对比)
- Java集合--深度剖析Vector、Stack、ArrayList、LinkedList(二)
- Java中的集合List、ArrayList、Vector、Stack(三)
- Java 集合:Collection,List,ArrayList,Vector,LinkedList(实现方式,对比)
- Java——(五)Collection之List集合、ArrayList和Vector实现类
- Java基础-集合 Collection,List,Arraylist,Vector
- Java之集合(四)Vector和Stack
- Java 集合:Collection,List,ArrayList,Vector,LinkedList(实现方式,对比)
- Java 集合:Collection,List,ArrayList,Vector,LinkedList(实现方式,对比)
- Java 7之集合类型 - Vector与Stack
- Java 集合 1:Collection,List,ArrayList,Vector,LinkedList(实现方式,对比)
- JAVA基础学习之String、StringBuffer、StringBuilder、基本数据类型的使用、整形进制转换、集合Collection、Vector、ArrayList、LinkedList、HashSet、TreeSet等(3)
- JAVA中的数据结构——集合类(线性表:Vector、Stack、LinkedList、set接口;键值对:Hashtable、Map接口<HashMap类、TreeMap类>)
- 集合框架:常用的集合类vector、Stack、List、set、Map,工具类Iterator、Collection
- Java 集合:Collection,List,ArrayList,Vector,LinkedList(实现方式,对比)
- Java 集合系列之 List总结(LinkedList, ArrayLis,vector,stack等使用场景和性能分析)
- java集合vector与stack
- Java 7之集合类型 - Vector与Stack