Pages

Friday, September 19, 2025

Java Collection Framework - Stack in Java

 import java.util.*;

// Java Collection Framework of Stack

class StackB {

    public static void main(String[] args) {

        Stack<Integer> s = new Stack<>();

        s.push(1);

        s.push(2);

        s.push(3);

        

        System.out.println(s); // Before pop

        

        while(!s.isEmpty()) {

            System.out.println(s.peek());

            s.pop();

        } 

        

        System.out.println(s);  // After pop 

    }

}

No comments:

Post a Comment

remove duplicates from sorted array - two pointer approach (leetcode)

  class Solution {     public int removeDuplicates ( int [] nums ) {         // base case: return if array have no el.         if ( nums...