Advertisement
nirobrasseen

Untitled

Jul 5th, 2022
697
325 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None
  1. package collection;
  2.  
  3. import java.util.Stack;
  4.  
  5. public class StackImplementation {
  6.     public static void main(String[] args) {
  7.         Stack<String> stringStack = new Stack<>();
  8.  
  9.         stringStack.push("Kamrul");  //Adding string by push
  10.         stringStack.push("Mou");
  11.         stringStack.push("Emon");
  12.         System.out.println("Initially: "+stringStack);
  13.  
  14.         int searchPosition = stringStack.search("Kamrul"); //Searching specific String.Returned Position number
  15.         System.out.println("Searching... "+searchPosition);
  16.        
  17.         stringStack.pop();  //Removing element
  18.         System.out.println("After removing top: "+stringStack);
  19.  
  20.         String peek = stringStack.peek(); //Removing upper elements without last one
  21.         System.out.println("After peeking "+peek);
  22.  
  23.         boolean empty = stringStack.empty();
  24.         System.out.println("Is Stack empty?: "+empty);
  25.  
  26.  
  27.     }
  28. }
  29.  
Advertisement
RAW Paste Data Copied
Advertisement