{"id":1259,"date":"2022-02-26T07:03:12","date_gmt":"2022-02-26T07:03:12","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1259"},"modified":"2022-02-26T07:03:12","modified_gmt":"2022-02-26T07:03:12","slug":"stack-class","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/26\/stack-class\/","title":{"rendered":"Stack Class"},"content":{"rendered":"\n<p>In this tutorial, we will learn about the Java Stack class and its methods with the help of examples.<\/p>\n\n\n\n<p>The Java collections framework has a class named&nbsp;<code>Stack<\/code>&nbsp;that provides the functionality of the stack data structure.<\/p>\n\n\n\n<p>The&nbsp;<code>Stack<\/code>&nbsp;class extends the&nbsp;<code>Vector<\/code>&nbsp;class.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-stack.png\" alt=\"Java Stack class extending the Vector class\" title=\"Java Stack Class\"\/><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"implementation\">Stack Implementation<\/h2>\n\n\n\n<p>In stack, elements are stored and accessed in&nbsp;<strong>Last In First Out<\/strong>&nbsp;manner. That is, elements are added to the top of the stack and removed from the top of the stack.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/stack-implementation.png\" alt=\"Working of stack data structure\" title=\"Working of stack data structure\"\/><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a Stack<\/h2>\n\n\n\n<p>In order to create a stack, we must import the&nbsp;<code>java.util.Stack<\/code>&nbsp;package first. Once we import the package, here is how we can create a stack in Java.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Stack&lt;Type&gt; stacks = new Stack&lt;&gt;();\n<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<code>Type<\/code>&nbsp;indicates the stack&#8217;s type. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Create Integer type stack\nStack&lt;Integer&gt; stacks = new Stack&lt;&gt;();\n\n\/\/ Create String type stack\nStack&lt;String&gt; stacks = new Stack&lt;&gt;();\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Stack Methods<\/h2>\n\n\n\n<p>Since\u00a0<code>Stack<\/code>\u00a0extends the\u00a0<code>Vector<\/code>\u00a0class, it inherits all the methods\u00a0<code>Vector<\/code>.<\/p>\n\n\n\n<p>Besides these methods, the&nbsp;<code>Stack<\/code>&nbsp;class includes 5 more methods that distinguish it from&nbsp;<code>Vector<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"push\">push() Method<\/h3>\n\n\n\n<p>To add an element to the top of the stack, we use the&nbsp;<code>push()<\/code>&nbsp;method. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Stack;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        Stack&lt;String&gt; animals= new Stack&lt;&gt;();\n\n        \/\/ Add elements to Stack\n        animals.push(\"Dog\");\n        animals.push(\"Horse\");\n        animals.push(\"Cat\");\n\n        System.out.println(\"Stack: \" + animals);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Stack: &#91;Dog, Horse, Cat]\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"pop\">pop() Method<\/h3>\n\n\n\n<p>To remove an element from the top of the stack, we use the&nbsp;<code>pop()<\/code>&nbsp;method. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Stack;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        Stack&lt;String&gt; animals= new Stack&lt;&gt;();\n\n        \/\/ Add elements to Stack\n        animals.push(\"Dog\");\n        animals.push(\"Horse\");\n        animals.push(\"Cat\");\n        System.out.println(\"Initial Stack: \" + animals);\n\n        \/\/ Remove element stacks\n        String element = animals.pop();\n        System.out.println(\"Removed Element: \" + element);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Initial Stack: &#91;Dog, Horse, Cat]\nRemoved Element: Cat\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"peek\">peek() Method<\/h3>\n\n\n\n<p>The&nbsp;<code>peek()<\/code>&nbsp;method returns an object from the top of the stack. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Stack;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        Stack&lt;String&gt; animals= new Stack&lt;&gt;();\n\n        \/\/ Add elements to Stack\n        animals.push(\"Dog\");\n        animals.push(\"Horse\");\n        animals.push(\"Cat\");\n        System.out.println(\"Stack: \" + animals);\n\n        \/\/ Access element from the top\n        String element = animals.peek();\n        System.out.println(\"Element at top: \" + element);\n\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Stack: &#91;Dog, Horse, Cat]\nElement at top: Cat\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"search\">search() Method<\/h3>\n\n\n\n<p>To search an element in the stack, we use the&nbsp;<code>search()<\/code>&nbsp;method. It returns the position of the element from the top of the stack. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Stack;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        Stack&lt;String&gt; animals= new Stack&lt;&gt;();\n\n        \/\/ Add elements to Stack\n        animals.push(\"Dog\");\n        animals.push(\"Horse\");\n        animals.push(\"Cat\");\n        System.out.println(\"Stack: \" + animals);\n\n        \/\/ Search an element\n        int position = animals.search(\"Horse\");\n        System.out.println(\"Position of Horse: \" + position);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Stack: &#91;Dog, Horse, Cat]\nPosition of Horse: 2\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"empty\">empty() Method<\/h3>\n\n\n\n<p>To check whether a stack is empty or not, we use the&nbsp;<code>empty()<\/code>&nbsp;method. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Stack;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        Stack&lt;String&gt; animals= new Stack&lt;&gt;();\n\n        \/\/ Add elements to Stack\n        animals.push(\"Dog\");\n        animals.push(\"Horse\");\n        animals.push(\"Cat\");\n        System.out.println(\"Stack: \" + animals);\n\n        \/\/ Check if stack is empty\n        boolean result = animals.empty();\n        System.out.println(\"Is the stack empty? \" + result);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Stack: &#91;Dog, Horse, Cat]\nIs the stack empty? false\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"deque\">Use ArrayDeque Instead of Stack<\/h2>\n\n\n\n<p>The&nbsp;<code>Stack<\/code>&nbsp;class provides the direct implementation of the stack data structure. However, it is recommended not to use it. Instead, use the&nbsp;<code>ArrayDeque<\/code>&nbsp;class (implements the&nbsp;<code>Deque<\/code>&nbsp;interface) to implement the stack data structure in Java.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about the Java Stack class and its methods with the help of examples. The Java collections framework has a class named&nbsp;Stack&nbsp;that provides the functionality of the stack data structure. The&nbsp;Stack&nbsp;class extends the&nbsp;Vector&nbsp;class. Stack Implementation In stack, elements are stored and accessed in&nbsp;Last In First Out&nbsp;manner. That is, elements are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[584],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1259"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1259"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1259\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}