The java.util.Stack.empty() method in Java is used to check whether a stack is empty or not. The method is of boolean type and returns true if the stack is empty else false.
Syntax:
STACK.empty()
Parameters: The method does not take any parameters.
Return Value: The method returns boolean true if the stack is empty else it returns false.
Below programs illustrate the working of java.util.Stack.empty() method:
Program 1:
// Java code to demonstrate empty() method import java.util.*;
public class Stack_Demo {
public static void main(String[] args)
{
// Creating an empty Stack
Stack<String> STACK = new Stack<String>();
// Stacking strings
STACK.push( "Geeks" );
STACK.push( "4" );
STACK.push( "Geeks" );
STACK.push( "Welcomes" );
STACK.push( "You" );
// Displaying the Stack
System.out.println( "The stack is: " + STACK);
// Checking for the emptiness of stack
System.out.println( "Is the stack empty? " +
STACK.empty());
// Popping out all the elements
STACK.pop();
STACK.pop();
STACK.pop();
STACK.pop();
STACK.pop();
// Checking for the emptiness of stack
System.out.println( "Is the stack empty? " +
STACK.empty());
}
} |
The stack is: [Geeks, 4, Geeks, Welcomes, You] Is the stack empty? false Is the stack empty? true
Program 2:
// Java code to demonstrate empty() method import java.util.*;
public class Stack_Demo {
public static void main(String[] args)
{
// Creating an empty Stack
Stack<Integer> STACK = new Stack<Integer>();
// Stacking int values
STACK.push( 8 );
STACK.push( 5 );
STACK.push( 9 );
STACK.push( 2 );
STACK.push( 4 );
// Displaying the Stack
System.out.println( "The stack is: " + STACK);
// Checking for the emptiness of stack
System.out.println( "Is the stack empty? " +
STACK.empty());
}
} |
The stack is: [8, 5, 9, 2, 4] Is the stack empty? false
Program 3: How to travel through Stack using java.util.Stack.empty() method to get sum of all elements
// Java code to demonstrate Traversal in Stack using empty() method import java.util.*;
public class Stack_Demo {
public static void main(String[] args)
{
// Creating an empty Stack
Stack<Integer> STACK = new Stack<>();
// You can also Initialize Stack as below
// both are same way
// Stack<Integer> STACK = new Stack<Integer>();
// Stack Pushing Elements
STACK.push( 23 );
STACK.push( 3 );
STACK.push(- 30 );
STACK.push( 13 );
STACK.push( 45 );
// Displaying the Stack
System.out.println( "The stack is: " + STACK);
// Initialize Sum Variable which will store sum of
// all element
int sum = 0 ;
// Popping till Stack is Not Empty
while (!STACK.empty()) {
/* If stack have some elements then
Stack.empty() will written false and ! of it
will true so while will till !STACK.empty() it
become false for this to be false STACK.empty()
need to be true when STACK.empty() is true mean
we have completely travel Stack*/
sum += STACK.pop();
}
// Initialize
System.out.println( "The Sum Of Elements is " + sum);
// Checking for the emptiness of stack
System.out.println( "Is the stack empty? "
+ STACK.empty());
}
// This Code is Contributed By Vikas Bishnoi
} |
The stack is: [23, 3, -30, 13, 45] The Sum Of Elements is 54 Is the stack empty? true
Program 4 : Use java.util.Stack.empty() method to implement error handling in program.
For example, if you are reading data from a file into a stack, you can check if the stack is empty after reading the data to ensure that the file is not empty.
// Java code to demonstrate error handling using empty() method import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Stack;
public class FileReadExample {
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
// read data from file
try {
File file = new File( "data.txt" );
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
stack.push(line);
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println( "File not found." );
}
// check if stack is empty
if (stack.empty()) {
System.out.println( "File is empty." );
} else {
System.out.println( "File is not empty. Stack contents:" );
while (!stack.empty()) {
System.out.println(stack.pop());
}
}
}
} // This code is contributed by vishalkumarsahu04 |
Time Complexity: O(n), where n is the number of lines in the file.
Space Complexity : O(n), where n is the number of lines in the file.