The Stack class represents LIFO - Last In First Out of objects. It extends class Vector. java.util.Stack is the package.Methods supported by Stack
push - Pushes an item onto the top of this stack.
Stack<String> stackFiles = new Stack<String>(); stackFiles.push("1"); stackFiles.push("2"); stackFiles.push("3"); stackFiles.push("4"); stackFiles.push("5");
pop - Removes the object at the top of the stack and returns it.
System.out.println( stackFiles.pop()); //prints 5
peek - Returns the top element of the stack but doesn't remove it.
System.out.println( stackFiles.pop()); //prints 5 System.out.println( stackFiles.peek()); //prints 4
empty - Returns true if the stack is empty or else false.
while( !stackFiles.empty() ){ System.out.println( stackFiles.pop() ); }
search - searches for the given element using equals and returns the depth of the element from the top of the stack. If not present returns -1.
Stack<String> stackFiles = new Stack<String>(); stackFiles.push("1"); stackFiles.push("2"); stackFiles.push("3"); stackFiles.push("4"); stackFiles.push("5"); System.out.println( stackFiles.search("5") ); //Prints 1 |