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());
}
}
|
Output
The 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());
}
}
|
Output
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
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());
}
}
|
Output
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
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.
Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
24 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...