Core Methods: push(), pop(), peek(), empty() – Thread Safety Notes
Core Methods in Stack Java – push(), pop(), peek(), empty() with Thread Safety Notes
अगर आप Java में Stack concept सीख रहे हैं, तो ये blog आपके लिए सबसे useful और exam-focused notes लेकर आया है। यहाँ हम Stack के चार core methods – push(), pop(), peek(), और empty() को पूरी तरह समझेंगे, साथ ही Thread Safety से जुड़ी important बातें भी जानेंगे। चलिए step by step समझते हैं।
What is Stack in Java?
Stack एक data structure है जो LIFO (Last In, First Out) principle पर काम करता है। यानी जो element सबसे आख़िर में insert किया जाता है, वही सबसे पहले remove होता है। Java में Stack को implement करने के लिए java.util.Stack class का use किया जाता है, जो internally Vector class को extend करती है।
Stack का use तब किया जाता है जब हमें elements को sequence के हिसाब से store और retrieve करना होता है, जैसे — function calls, undo operations, expression evaluation आदि में।
Example of Stack declaration
Stack<Integer> stack = new Stack<>();
Core Methods of Stack in Java
अब बात करते हैं Stack के चार सबसे important methods की — push(), pop(), peek(), और empty()। ये methods Stack के basic operations को handle करते हैं।
1. push() Method
push() method Stack में नया element add करने के लिए use किया जाता है। जो element add होता है, वो Stack के top पर जाता है।
Syntax:
stack.push(element);
Example:
Stack<String> stack = new Stack<>();
stack.push("Java");
stack.push("Python");
stack.push("C++");
System.out.println(stack); // Output: [Java, Python, C++]
ऊपर दिए example में “C++” सबसे आख़िर में push हुआ है, इसलिए वही Stack के top पर है।
2. pop() Method
pop() method Stack से top element को remove करने के लिए use किया जाता है। अगर Stack खाली (empty) है और आप pop() call करते हैं, तो ये EmptyStackException throw करता है।
Syntax:
stack.pop();
Example:
Stack<String> stack = new Stack<>();
stack.push("A");
stack.push("B");
stack.push("C");
System.out.println(stack.pop()); // Output: C
System.out.println(stack); // Output: [A, B]
यहाँ "C" remove हो गया क्योंकि वो Stack के top पर था।
3. peek() Method
peek() method Stack के top element को return करता है, लेकिन उसे remove नहीं करता। यानी आप सिर्फ़ top element को देख सकते हैं कि कौन सा है।
Syntax:
stack.peek();
Example:
Stack<Integer> numbers = new Stack<>();
numbers.push(10);
numbers.push(20);
System.out.println(numbers.peek()); // Output: 20
System.out.println(numbers); // Output: [10, 20]
यहाँ “20” Stack के top पर है, इसलिए peek() method ने वही return किया।
4. empty() Method
empty() method यह check करने के लिए use किया जाता है कि Stack खाली है या नहीं। यह method true return करता है अगर Stack में कोई element नहीं है, और false अगर Stack में elements मौजूद हैं।
Syntax:
stack.empty();
Example:
Stack<String> stack = new Stack<>();
System.out.println(stack.empty()); // true
stack.push("Java");
System.out.println(stack.empty()); // false
यह method बहुत useful है जब आपको Stack operations से पहले empty state check करनी हो।
Example using all core methods together
import java.util.Stack;
public class StackExample {
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
stack.push("Apple");
stack.push("Banana");
stack.push("Cherry");
System.out.println("Stack elements: " + stack);
System.out.println("Top element (peek): " + stack.peek());
System.out.println("Removed element (pop): " + stack.pop());
System.out.println("Is Stack empty? " + stack.empty());
}
}
Output:
Stack elements: [Apple, Banana, Cherry]
Top element (peek): Cherry
Removed element (pop): Cherry
Is Stack empty? false
Stack Methods Summary Table
| Method | Description (Hindi + English) | Return Type | Throws Exception |
|---|---|---|---|
| push(E item) | Stack में नया element add करता है। | E | – |
| pop() | Top element को remove और return करता है। | E | EmptyStackException |
| peek() | Top element को return करता है बिना remove किए। | E | EmptyStackException |
| empty() | Check करता है कि Stack खाली है या नहीं। | boolean | – |
Thread Safety in Stack
अब बात करते हैं Thread Safety की। Java में Stack class synchronized है क्योंकि यह Vector class को extend करती है। इसका मतलब यह है कि Stack का हर method internally synchronized होता है और multiple threads से access होने पर भी data corruption नहीं होता।
लेकिन ध्यान रखें:
- अगर performance ज़्यादा important है और thread safety की ज़रूरत नहीं है, तो ArrayDeque या LinkedList को Stack की जगह use करना बेहतर रहेगा।
- अगर multi-threading environment में काम कर रहे हैं, तो Stack safe है लेकिन थोड़ा slow हो सकता है।
Modern Alternative (Recommended)
Java में अब Stack के बजाय Deque interface (जैसे ArrayDeque) का use करना recommended है क्योंकि ये ज्यादा fast और flexible होता है।
Deque<Integer> stack = new ArrayDeque<>();
stack.push(10);
stack.push(20);
System.out.println(stack.pop()); // Output: 20
यह implementation Stack की तरह ही काम करता है लेकिन unsynchronized होता है, इसलिए single-threaded applications में बेहतर performance देता है।
When to use Stack in Java
Stack का use तब किया जाता है जब आपको LIFO structure चाहिए। कुछ common examples:
- Function call management (call stack)
- Undo/Redo feature in text editors
- Expression evaluation (Postfix, Prefix)
- Syntax parsing
Advantages and Disadvantages of Stack
| Advantages | Disadvantages |
|---|---|
|
|
Important Points to Remember
- Stack class Java की Collection Framework का हिस्सा है।
- Stack internally Vector का use करता है।
- All four methods – push(), pop(), peek(), empty() — synchronized हैं।
- EmptyStackException तभी throw होती है जब Stack खाली हो और आप pop() या peek() call करें।
- Modern Java में ArrayDeque को Stack की जगह use करने की सलाह दी जाती है।
Short Notes for Exam
- push() – Stack के top पर नया element add करता है।
- pop() – Top element remove करता है और return करता है।
- peek() – Top element को return करता है बिना remove किए।
- empty() – Check करता है कि Stack खाली है या नहीं।
- Thread Safety – Stack synchronized होता है, इसलिए multi-threading में safe है।
- Alternative – Better performance के लिए
ArrayDequeuse करें।
Exam Oriented Summary
अगर आपको college exams या interview के लिए Stack methods याद रखने हैं, तो नीचे दिए short formula याद रखें:
- PUSH – Add element
- POP – Remove top element
- PEEK – View top element
- EMPTY – Check if stack is empty
- THREAD SAFE – Yes, because synchronized