Stack类
可以看你到stack的源码很少,它是基于Vector集合来实现的,而Vector集合本来就是线程安全的,使用Synchronize的方式实现的,效率比较低,所以不建议使用。
public
class Stack<E> extends Vector<E> {
/**
* Creates an empty Stack.
*/
public Stack() {
}
/**
* Pushes an item onto the top of this stack. This has exactly
* the same effect as:
* <blockquote><pre>
* addElement(item)</pre></blockquote>
*
* @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;
}
/**
* Removes the object at the top of this stack and returns that
* object as the value of this function.
*
* @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;
}
/**
* Looks at the object at the top of this stack without removing it
* from the stack.
*
* @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;
}
/**
* Returns the 1-based position where an object is on this stack.
* If the object <tt>o</tt> occurs as an item in this stack, this
* method returns the distance from the top of the stack of the
* occurrence nearest the top of the stack; the topmost item on the
* stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt>
* method is used to compare <tt>o</tt> to the
* items in this stack.
*
* @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;
可以使用双端队列代替Stack,效果更好(参考)
使用方式
public static void main(String[] args) throws IOException {
Stack<Integer> stack = new Stack<>();
stack.push(1);
ArrayDeque<Integer> deque = new ArrayDeque<>();
deque.push(1);
deque.push(2);
deque.push(3);
deque.push(4);
deque.offerFirst(5);
deque.offerFirst(6);
deque.push(7);
deque.push(8);
while (!deque.isEmpty()){
System.out.println(deque.pop());
}
//8 7 6 5 4 3 2 1
}
public static void main(String[] args) throws IOException {
Stack<Integer> stack = new Stack<>();
stack.push(1);
ArrayDeque<Integer> deque = new ArrayDeque<>();
deque.push(1);
deque.push(2);
deque.push(3);
deque.push(4);
deque.offerLast(5);
deque.offerLast(6);
deque.push(7);
deque.push(8);
while (!deque.isEmpty()){
System.out.println(deque.pop());
}
//8 7 4 3 2 1 5 6
}
ArrayDeque底层通过数组实现,为了满足可以同时在数组两端插入或删除元素的需求,该数组还必须是循环的,即循环数组(circular array),也就是说数组的任何一点都可能被看作起点或者终点。ArrayDeque是非线程安全的(not thread-safe),当多个线程同时使用的时候,需要程序员手动同步;另外,该容器不允许放入null元素。
双向链表实现双端队列
LinkedList<Integer> list = new LinkedList<>();
list.push(1);
list.push(2);
list.push(3);
list.push(4);
list.offerLast(5);
list.offerLast(6);
list.push(7);
list.push(8);
while (!list.isEmpty()){
System.out.println(list.pop());
}
由于 LinkedList 基于链表实现,存储元素过程中,无需像 ArrayList 那样进行扩容。但有得必有失,LinkedList 存储元素的节点需要额外的空间存储前驱和后继的引用。另一方面,LinkedList 在链表头部和尾部插入效率比较高,但在指定位置进行插入时,效率一般。原因是,在指定位置插入需要定位到该位置处的节点,此操作的时间复杂度为 O(N)。
同时,LinkedList也是非线程安全的,并发环境下,多个线程同时操作 LinkedList,会引发不可预知的错误。