English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
java Collection Stack
Summary:
Stack is a stack, its characteristic is First In Last Out (FILO). Stack inherits from Vector (vector queue), since Vector is implemented by array, Stack also uses array rather than linked list.
The relationship between Stack and Collection is as follows:
based on Java8source code:
public class Stack<E> extends Vector<E> { public Stack() {//create an empty stack } public E push(E item) {//push addElement(item); return item; } //pop public synchronized E pop() { E obj; int len = size(); obj = peek(); removeElementAt(len - 1); return obj; } //return the top element of the stack without removing it public synchronized E peek() { int len = size(); if (len == 0) throw new EmptyStackException(); return elementAt(len - 1); } //determine if the stack is empty public boolean empty() { return size() == 0; } //find element and return stack depth public synchronized int search(Object o) { int i = lastIndexOf(o); if (i >= 0) { return size() - i; } return -1; } //serial version ID private static final long serialVersionUID = 1224463164541339165L; }
Thank you for reading, I hope it can help everyone, thank you for your support to this site!