java1.7集合源码阅读: Stack
Stack类也是List接口的一种实现,也是一个有着非常长历史的实现,从jdk1.0开始就有了这个实现。
Stack是一种基于后进先出队列的实现(last-in-first-out (LIFO)),实际上jdk也提供了有关队列的其他实现,这里就先看看Stack的实现:
类定义:
public class Stack<E> extends Vector<E> { //从类定义看,Stack是线程安全的 ..... }
看看Stack提供的一些CRUD方法:
1 /** 2 * Pushes an item onto the top of this stack. This has exactly 3 * the same effect as: 4 * <blockquote><pre> 5 * addElement(item)</pre></blockquote> 6 * 7 * @param item the item to be pushed onto this stack. 8 * @return the <code>item</code> argument. 9 * @see java.util.Vector#addElement 10 */ 11 public E push(E item) { // push方法是在队尾增加一个元素 12 addElement(item); 13 14 return item; 15 } 16 17 /** 18 * Adds the specified component to the end of this vector, 19 * increasing its size by one. The capacity of this vector is 20 * increased if its size becomes greater than its capacity. 21 * 22 * <p>This method is identical in functionality to the 23 * {@link #add(Object) add(E)} 24 * method (which is part of the {@link List} interface). 25 * 26 * @param obj the component to be added 27 */ 28 public synchronized void addElement(E obj) { //Vector中的方法 29 modCount++; 30 ensureCapacityHelper(elementCount + 1); 31 elementData[elementCount++] = obj; 32 }
重点注意一下pop方法:
1 /** 2 * Removes the object at the top of this stack and returns that 3 * object as the value of this function. 4 * 5 * @return The object at the top of this stack (the last item 6 * of the <tt>Vector</tt> object). 7 * @throws EmptyStackException if this stack is empty. 8 */ 9 public synchronized E pop() { //pop方法是获取并删除队尾的元素 10 E obj; 11 int len = size(); 12 13 obj = peek(); //见后续peek()方法 14 removeElementAt(len - 1); 15 16 return obj; 17 } 18 /** 19 * Deletes the component at the specified index. Each component in 20 * this vector with an index greater or equal to the specified 21 * {@code index} is shifted downward to have an index one 22 * smaller than the value it had previously. The size of this vector 23 * is decreased by {@code 1}. 24 * 25 * <p>The index must be a value greater than or equal to {@code 0} 26 * and less than the current size of the vector. 27 * 28 * <p>This method is identical in functionality to the {@link #remove(int)} 29 * method (which is part of the {@link List} interface). Note that the 30 * {@code remove} method returns the old value that was stored at the 31 * specified position. 32 * 33 * @param index the index of the object to remove 34 * @throws ArrayIndexOutOfBoundsException if the index is out of range 35 * ({@code index < 0 || index >= size()}) 36 */ 37 public synchronized void removeElementAt(int index) { //Vector中的方法,删除指定index元素 38 modCount++; 39 if (index >= elementCount) { 40 throw new ArrayIndexOutOfBoundsException(index + " >= " + 41 elementCount); 42 } 43 else if (index < 0) { 44 throw new ArrayIndexOutOfBoundsException(index); 45 } 46 int j = elementCount - index - 1; 47 if (j > 0) { 48 System.arraycopy(elementData, index + 1, elementData, index, j); 49 } 50 elementCount--; 51 elementData[elementCount] = null; 52 }
Stack 的peek方法,只是获取元素,但并不会去做删除处理,当队列中没有元素的时候,会报EmptyStackException异常:
public synchronized E peek() { int len = size(); if (len == 0) throw new EmptyStackException(); return elementAt(len - 1); }
Stack 比较简单,大部分实现都在Vector中,可参考:java1.7集合源码阅读: Vector