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
import java.util.*;
public class Stack_Demo {
public static void main(String[] args)
{
Stack<String> STACK = new Stack<String>();
STACK.push( "Geeks" );
STACK.push( "4" );
STACK.push( "Geeks" );
STACK.push( "Welcomes" );
STACK.push( "You" );
System.out.println( "The stack is: " + STACK);
System.out.println( "Is the stack empty? " +
STACK.empty());
STACK.pop();
STACK.pop();
STACK.pop();
STACK.pop();
STACK.pop();
System.out.println( "Is the stack empty? " +
STACK.empty());
}
}
|
OutputThe stack is: [Geeks, 4, Geeks, Welcomes, You]
Is the stack empty? false
Is the stack empty? true
Program 2:
Java
import java.util.*;
public class Stack_Demo {
public static void main(String[] args)
{
Stack<Integer> STACK = new Stack<Integer>();
STACK.push( 8 );
STACK.push( 5 );
STACK.push( 9 );
STACK.push( 2 );
STACK.push( 4 );
System.out.println( "The stack is: " + STACK);
System.out.println( "Is the stack empty? " +
STACK.empty());
}
}
|
OutputThe 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
import java.util.*;
public class Stack_Demo {
public static void main(String[] args)
{
Stack<Integer> STACK = new Stack<>();
STACK.push( 23 );
STACK.push( 3 );
STACK.push(- 30 );
STACK.push( 13 );
STACK.push( 45 );
System.out.println( "The stack is: " + STACK);
int sum = 0 ;
while (!STACK.empty()) {
sum += STACK.pop();
}
System.out.println( "The Sum Of Elements is " + sum);
System.out.println( "Is the stack empty? "
+ STACK.empty());
}
}
|
OutputThe 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
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<>();
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." );
}
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());
}
}
}
}
|
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.