Java Program to Implement Stack API Last Updated : 27 Jul, 2022 Comments Improve Suggest changes Like Article Like Report A stack is a linear data structure that follows a particular order in which insertion/deletion operations are performed. The order is either LIFO(Last In First Out) or FILO(First In Last Out). Stack uses the push() function in order to insert new elements into the Stack and pop() function in order to remove an element from the stack. Insertion and removal in the stack are allowed at only one end called Top. Overflow state in the stack occurs when it is completely full and Underflow state in the stack occurs when it is completely empty. Example: Input: stack.push(1) stack.push(2) stack.pop() stack.peek() Output: 2 2Syntax: public class Stack<E> extends Vector<E>Stack API implements: Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess.Methods in Stack: empty() - Tests if this stack is empty.peek() - Looks at the object at the top of this stack without removing it from the stack.pop() - Removes the object at the top of this stack and returns that object as the value of this function.push(E item) - Pushes an item onto the top of this stack.int search(Object o) - Returns the 1-based position where an object is on this stack.Below is the implementation of the problem statement: Java // Java program to implement Stack API import java.util.Stack; public class StackImpl<E> { private Stack<E> stack; // Constructor to create empty Stack. public StackImpl() { stack = new Stack<E>(); } // method to check if stack is empty or not. public boolean empty() { return stack.empty(); } // method to return topmost element of stack public E peek() { return stack.peek(); } // method to remove and return topmost element of stack public E pop() { return stack.pop(); } // method to push an element into the stack public E push(E item) { return stack.push(item); } // method to return the position of an object // in a stack(1-based position) public int search(Object o) { return stack.search(o); } public static void main(String args[]) { StackImpl<String> stack = new StackImpl<String>(); System.out.println("element pushed : " + stack.push("one")); System.out.println("element pushed : " + stack.push("two")); System.out.println("element pushed : " + stack.push("three")); System.out.println("element pushed : " + stack.push("four")); System.out.println("element pushed : " + stack.push("five")); System.out.println("element popped : " + stack.pop()); System.out.println("element popped : " + stack.pop()); System.out.println("Element peek : " + stack.peek()); System.out.println("position of element three - " + stack.search("three")); while (!stack.empty()) { System.out.println("element popped : " + stack.pop()); } } } Outputelement pushed : one element pushed : two element pushed : three element pushed : four element pushed : five element popped : five element popped : four Element peek : three position of element three - 1 element popped : three element popped : two element popped : one Comment More infoAdvertise with us Next Article Java Program to Implement Stack API G gunjanpaul Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-Stack +1 More Practice Tags : Java Similar Reads How to Implement Stack in Java Using Array and Generics? Stack is a linear Data Structure that is based on the LIFO concept (last in first out). Instead of only an Integer Stack, Stack can be of String, Character, or even Float type. There are 4 primary operations in the stack as follows: push() Method adds element x to the stack.pop() Method removes the 5 min read Java Threading Programs - Basic to Advanced Java threading is the concept of using multiple threads to execute different tasks in a Java program. A thread is a lightweight sub-process that runs within a process and shares the same memory space and resources. Threads can improve the performance and responsiveness of a program by allowing paral 3 min read How to Implement Stack in Java using LinkedList and Generics? Prerequisites: Generics in JavaLinkedList in JavaWhat is Stack? Stack is a linear Data Structure that follows LIFO(Last In First Out) order while performing its operations. The main operations that are performed on the stack are mentioned below: push() : Â inserts an element at beginning of the stack 6 min read Implementing Inorder, Preorder, Postorder Using Stack in Java Data structures of two types of Linear Data Structure and the second is Non-linear Data Structure the main difference between these Data structures is the way of transverse the elements of these data structures. Linear Data Structure Transverse all elements in a single run whereas Non-Linear Data st 8 min read How to Use Deque as a Stack in Java? The Deque extends the Queue interface and provides additional methods to support operation at both ends. This interface is part of the java.util package. The Deque interface does not strictly enforce the order of elements, meaning that different implementations may behave differently. The standard i 2 min read Java Networking Programs - Basic to Advanced Java allows developers to create applications that can communicate over networks, connecting devices and systems together. Whether you're learning about basic connections or diving into more advanced topics like client-server applications, Java provides the tools and libraries you need. This Java Ne 3 min read Calculator Using RMI(Remote Method Invocation) in Java RMI (Remote Method Invocation) is an API used to access objects running on another JVM(Server-side). It is mainly used for the creation of distributed systems and is provided in Java Rome. Stub and Skeleton are the two objects used for handling communication between client and server. The following 3 min read Java Program to Implement Stack Data Structure Stack is the fundamental Data Structure that can follow the Last In, First Out(LIFO) principle. It can work that the last element added to the stack will be the first one to be removed. It can operate like a stack of plates: We can only add or remove the topmost plate at any given time. The simplici 5 min read C++ Program to Implement Stack using array Stack is the fundamental data structure that can operates the under the Last In, First Out (LIFO) principle. This means that the last element added to the stack is the first one to be removed. Implementing the stack using the array is one of the most straightforward methods in the terms of the both 4 min read Implement a Stack in C Programming Stack is the linear data structure that follows the Last in, First Out(LIFO) principle of data insertion and deletion. It means that the element that is inserted last will be the first one to be removed and the element that is inserted first will be removed at last. Think of it as the stack of plate 7 min read Like