SlideShare a Scribd company logo
2
Most read
3
Most read
8
Most read
Java Stack Data Structure
Java Stack Class
 The Java collections framework has a class
named Stack that provides the functionality of the
stack data structure.
 The Stack class extends the Vector class.
Stack Implementation
 In stack, elements are stored and accessed in Last In First
Out manner. That is, elements are added to the top of the stack
and removed from the top of the stack.
Creating a Stack
 In order to create a stack, we must import
the java.util.Stack package first. Once we import the
package, here is how we can create a stack in Java.
Stack<Type> stacks = new Stack<>();
Here, Type indicates the stack's type. For example,
// Create Integer type stack
Stack<Integer> stacks = new Stack<>();
// Create String type stack
Stack<String> stacks = new Stack<>();
Stack Methods
 Since Stack extends the Vector class, it inherits
all the methods Vector.
 Besides these methods, the Stack class includes
5 more methods that distinguish it from Vector.
push() Method
 To add an element to the top of the stack, we use
the push() method. For example
import java.util.Stack;
class Main { public static void main(String[] args)
{ Stack<String> animals= new Stack<>();
// Add elements to Stack animals.push("Dog");
animals.push("Horse");
animals.push("Cat");
System.out.println("Stack: " + animals);
}
}
pop() Method
import java.util.Stack;
class Main
{
public static void main(String[] args)
{ Stack<String> animals= new Stack<>();
// Add elements to Stack
animals.push("Dog");
animals.push("Horse");
animals.push("Cat");
System.out.println("Initial Stack: " + animals);
// Remove element stacks
String element = animals.pop();
System.out.println("Removed Element: " + element); } }
search() Method
 to search an element in the stack, we use the search() method. It returns the position of the
element from the top of the stack. For example,
import java.util.Stack;
class Main
{
public static void main(String[] args)
{
Stack<String> animals= new Stack<>();
// Add elements to Stack
animals.push("Dog");
animals.push("Horse");
animals.push("Cat");
System.out.println("Stack: " + animals);
// Search an elemen
t int position = animals.search("Horse");
System.out.println("Position of Horse: " + position); } }
empty() Method
 To check whether a stack is empty or not, we use the empty() method.
For example,
import java.util.Stack;
class Main
{
public static void main(String[] args)
{
Stack<String> animals= new Stack<>();
// Add elements to Stack
animals.push("Dog");
animals.push("Horse");
animals.push("Cat");
System.out.println("Stack: " + animals);
// Check if stack is empty
boolean result = animals.empty();
System.out.println("Is the stack empty? " + result); } }

More Related Content

PPTX
Java Stack Data Structure.pptx
PPT
stack and queue array implementation in java.
PDF
Data Structures Notes 2021
PPT
Stack a Data Structure
PPTX
DSA in java
PPTX
Java Queue.pptx
PPTX
Data structures
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Java Stack Data Structure.pptx
stack and queue array implementation in java.
Data Structures Notes 2021
Stack a Data Structure
DSA in java
Java Queue.pptx
Data structures
Chapter 02: Classes Objects and Methods Java by Tushar B Kute

What's hot (20)

PDF
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
PPTX
PPT
Java interfaces
PPTX
JAVA AWT
PPS
Wrapper class
PDF
Collections in Java Notes
PPT
Basic concept of OOP's
PPTX
Java string handling
PPTX
Inheritance in java
PDF
Command line-arguments-in-java-tutorial
PPT
Java collections concept
PPS
Java Exception handling
PPTX
Classes, objects in JAVA
PPTX
Core Java Tutorials by Mahika Tutorials
PPT
Oops ppt
PPTX
Arrays in java
PPTX
Java Beans
PPTX
Event handling
PPTX
Command line arguments
PPTX
Java beans
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Java interfaces
JAVA AWT
Wrapper class
Collections in Java Notes
Basic concept of OOP's
Java string handling
Inheritance in java
Command line-arguments-in-java-tutorial
Java collections concept
Java Exception handling
Classes, objects in JAVA
Core Java Tutorials by Mahika Tutorials
Oops ppt
Arrays in java
Java Beans
Event handling
Command line arguments
Java beans
Ad

Similar to Java Stack Data Structure.pptx (20)

PPT
An Introduction to Stack Data Structures
PPT
introduction stacks in data structures and algorithms
PPT
MODULE 01-INTRODUCTION TO STACK-PRESENTATION
PPT
introduction to stack data structures chapter 1
PPT
01-intro_stacks.ppt
PPT
2 a stacks
PPT
Stack Implementation
PPTX
Java Programming Comprehensive Guide.pptx
PDF
PPT
Stacks_As_Advanced_Data_Strcutures11.ppt
PPTX
IT2070 Lecture 01 2021.pptx
PPT
PPTX
Data Structure - Stack.pptx
PPTX
Stack & Queue
DOCX
(674335607) cs2309 java-lab-manual
PPTX
Collection and framework
An Introduction to Stack Data Structures
introduction stacks in data structures and algorithms
MODULE 01-INTRODUCTION TO STACK-PRESENTATION
introduction to stack data structures chapter 1
01-intro_stacks.ppt
2 a stacks
Stack Implementation
Java Programming Comprehensive Guide.pptx
Stacks_As_Advanced_Data_Strcutures11.ppt
IT2070 Lecture 01 2021.pptx
Data Structure - Stack.pptx
Stack & Queue
(674335607) cs2309 java-lab-manual
Collection and framework
Ad

More from vishal choudhary (20)

PPTX
mobile application using automatin using node ja java on
PPTX
mobile development using node js and java
PPTX
Pixel to Percentage conversion Convert left and right padding of a div to per...
PPTX
esponsive web design means that your website (
PPTX
function in php using like three type of function
PPTX
data base connectivity in php using msql database
PPTX
software evelopment life cycle model and example of water fall model
PPTX
software Engineering lecture on development life cycle
PPTX
strings in php how to use different data types in string
PPTX
OPEN SOURCE WEB APPLICATION DEVELOPMENT question
PPTX
web performnace optimization using css minification
PPTX
web performance optimization using style
PPTX
Data types and variables in php for writing and databse
PPTX
Data types and variables in php for writing
PPTX
Data types and variables in php for writing
PPTX
sofwtare standard for test plan it execution
PPTX
Software test policy and test plan in development
PPTX
function in php like control loop and its uses
PPTX
introduction to php and its uses in daily
PPTX
data type in php and its introduction to use
mobile application using automatin using node ja java on
mobile development using node js and java
Pixel to Percentage conversion Convert left and right padding of a div to per...
esponsive web design means that your website (
function in php using like three type of function
data base connectivity in php using msql database
software evelopment life cycle model and example of water fall model
software Engineering lecture on development life cycle
strings in php how to use different data types in string
OPEN SOURCE WEB APPLICATION DEVELOPMENT question
web performnace optimization using css minification
web performance optimization using style
Data types and variables in php for writing and databse
Data types and variables in php for writing
Data types and variables in php for writing
sofwtare standard for test plan it execution
Software test policy and test plan in development
function in php like control loop and its uses
introduction to php and its uses in daily
data type in php and its introduction to use

Recently uploaded (20)

PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Computing-Curriculum for Schools in Ghana
PPTX
master seminar digital applications in india
PPTX
Cell Structure & Organelles in detailed.
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Sports Quiz easy sports quiz sports quiz
PDF
01-Introduction-to-Information-Management.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Computing-Curriculum for Schools in Ghana
master seminar digital applications in india
Cell Structure & Organelles in detailed.
PPH.pptx obstetrics and gynecology in nursing
Supply Chain Operations Speaking Notes -ICLT Program
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Sports Quiz easy sports quiz sports quiz
01-Introduction-to-Information-Management.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Microbial diseases, their pathogenesis and prophylaxis
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
human mycosis Human fungal infections are called human mycosis..pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
STATICS OF THE RIGID BODIES Hibbelers.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Complications of Minimal Access Surgery at WLH
GDM (1) (1).pptx small presentation for students
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Final Presentation General Medicine 03-08-2024.pptx

Java Stack Data Structure.pptx

  • 1. Java Stack Data Structure
  • 2. Java Stack Class  The Java collections framework has a class named Stack that provides the functionality of the stack data structure.  The Stack class extends the Vector class.
  • 3. Stack Implementation  In stack, elements are stored and accessed in Last In First Out manner. That is, elements are added to the top of the stack and removed from the top of the stack.
  • 4. Creating a Stack  In order to create a stack, we must import the java.util.Stack package first. Once we import the package, here is how we can create a stack in Java. Stack<Type> stacks = new Stack<>(); Here, Type indicates the stack's type. For example, // Create Integer type stack Stack<Integer> stacks = new Stack<>(); // Create String type stack Stack<String> stacks = new Stack<>();
  • 5. Stack Methods  Since Stack extends the Vector class, it inherits all the methods Vector.  Besides these methods, the Stack class includes 5 more methods that distinguish it from Vector.
  • 6. push() Method  To add an element to the top of the stack, we use the push() method. For example import java.util.Stack; class Main { public static void main(String[] args) { Stack<String> animals= new Stack<>(); // Add elements to Stack animals.push("Dog"); animals.push("Horse"); animals.push("Cat"); System.out.println("Stack: " + animals); } }
  • 7. pop() Method import java.util.Stack; class Main { public static void main(String[] args) { Stack<String> animals= new Stack<>(); // Add elements to Stack animals.push("Dog"); animals.push("Horse"); animals.push("Cat"); System.out.println("Initial Stack: " + animals); // Remove element stacks String element = animals.pop(); System.out.println("Removed Element: " + element); } }
  • 8. search() Method  to search an element in the stack, we use the search() method. It returns the position of the element from the top of the stack. For example, import java.util.Stack; class Main { public static void main(String[] args) { Stack<String> animals= new Stack<>(); // Add elements to Stack animals.push("Dog"); animals.push("Horse"); animals.push("Cat"); System.out.println("Stack: " + animals); // Search an elemen t int position = animals.search("Horse"); System.out.println("Position of Horse: " + position); } }
  • 9. empty() Method  To check whether a stack is empty or not, we use the empty() method. For example, import java.util.Stack; class Main { public static void main(String[] args) { Stack<String> animals= new Stack<>(); // Add elements to Stack animals.push("Dog"); animals.push("Horse"); animals.push("Cat"); System.out.println("Stack: " + animals); // Check if stack is empty boolean result = animals.empty(); System.out.println("Is the stack empty? " + result); } }