{"id":1265,"date":"2022-02-26T07:09:14","date_gmt":"2022-02-26T07:09:14","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1265"},"modified":"2022-02-26T07:09:14","modified_gmt":"2022-02-26T07:09:14","slug":"deque-interface","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/26\/deque-interface\/","title":{"rendered":"Deque Interface"},"content":{"rendered":"\n<p>In this tutorial, we will learn about the Deque interface, how to use it, and its methods.<\/p>\n\n\n\n<p>The&nbsp;<code>Deque<\/code>&nbsp;interface of the Java collections framework provides the functionality of a double-ended queue. It extends the&nbsp;<code>Queue<\/code>&nbsp;interface.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"working\">Working of Deque<\/h2>\n\n\n\n<p>In a regular queue, elements are added from the rear and removed from the front. However, in a deque, we can&nbsp;<strong>insert and remove elements from both front and rear<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/working-of-deque.png\" alt=\"Working of deque (double-ended queue) data structure\"\/><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"classes\">Classes that implement Deque<\/h2>\n\n\n\n<p>In order to use the functionalities of the&nbsp;<code>Deque<\/code>&nbsp;interface, we need to use classes that implement it:<\/p>\n\n\n\n<ul><li>ArrayDeque<\/li><li>LinkedList<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/deque-implemention-classes.png\" alt=\"ArrayDeque and Linkedlist implements Deque\"\/><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"use\">How to use Deque?<\/h2>\n\n\n\n<p>In Java, we must import the&nbsp;<code>java.util.Deque<\/code>&nbsp;package to use&nbsp;<code>Deque<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Array implementation of Deque\nDeque&lt;String&gt; animal1 = new ArrayDeque&lt;&gt;();\n\n\/\/ LinkedList implementation of Deque\nDeque&lt;String&gt; animal2 = new LinkedList&lt;&gt;();\n<\/code><\/pre>\n\n\n\n<p>Here, we have created objects&nbsp;<var>animal1<\/var>&nbsp;and&nbsp;<var>animal2<\/var>&nbsp;of classes&nbsp;<var>ArrayDeque<\/var>&nbsp;and&nbsp;<var>LinkedList<\/var>, respectively. These objects can use the functionalities of the&nbsp;<code>Deque<\/code>&nbsp;interface.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"methods\">Methods of Deque<\/h2>\n\n\n\n<p>Since\u00a0<code>Deque<\/code>\u00a0extends the\u00a0<code>Queue<\/code>\u00a0interface, it inherits all the methods of\u00a0the Queue interface.<\/p>\n\n\n\n<p>Besides methods available in the&nbsp;<code>Queue<\/code>&nbsp;interface, the&nbsp;<code>Deque<\/code>&nbsp;interface also includes the following methods:<\/p>\n\n\n\n<ul><li><strong>addFirst()<\/strong>&nbsp;&#8211; Adds the specified element at the beginning of the deque. Throws an exception if the deque is full.<\/li><li><strong>addLast()<\/strong>&nbsp;&#8211; Adds the specified element at the end of the deque. Throws an exception if the deque is full.<\/li><li><strong>offerFirst()<\/strong>&nbsp;&#8211; Adds the specified element at the beginning of the deque. Returns&nbsp;<code>false<\/code>&nbsp;if the deque is full.<\/li><li><strong>offerLast()<\/strong>&nbsp;&#8211; Adds the specified element at the end of the deque. Returns&nbsp;<code>false<\/code>&nbsp;if the deque is full.<\/li><li><strong>getFirst()<\/strong>&nbsp;&#8211; Returns the first element of the deque. Throws an exception if the deque is empty.<\/li><li><strong>getLast()<\/strong>&nbsp;&#8211; Returns the last element of the deque. Throws an exception if the deque is empty.<\/li><li><strong>peekFirst()<\/strong>&nbsp;&#8211; Returns the first element of the deque. Returns&nbsp;<code>null<\/code>&nbsp;if the deque is empty.<\/li><li><strong>peekLast()<\/strong>&nbsp;&#8211; Returns the last element of the deque. Returns&nbsp;<code>null<\/code>&nbsp;if the deque is empty.<\/li><li><strong>removeFirst()<\/strong>&nbsp;&#8211; Returns and removes the first element of the deque. Throws an exception if the deque is empty.<\/li><li><strong>removeLast()<\/strong>&nbsp;&#8211; Returns and removes the last element of the deque. Throws an exception if the deque is empty.<\/li><li><strong>pollFirst()<\/strong>&nbsp;&#8211; Returns and removes the first element of the deque. Returns&nbsp;<code>null<\/code>&nbsp;if the deque is empty.<\/li><li><strong>pollLast()<\/strong>&nbsp;&#8211; Returns and removes the last element of the deque. Returns&nbsp;<code>null<\/code>&nbsp;if the deque is empty.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"stack\">Deque as Stack Data Structure<\/h2>\n\n\n\n<p>The&nbsp;<code>Stack<\/code>&nbsp;class of the Java&nbsp;<code>Collections<\/code>&nbsp;framework provides the implementation of the stack.<\/p>\n\n\n\n<p>However, it is recommended to use\u00a0<code>Deque<\/code>\u00a0as a stack instead of\u00a0the Stack class. It is because methods of\u00a0<code>Stack<\/code>\u00a0are synchronized.<\/p>\n\n\n\n<p>Here are the methods the&nbsp;<code>Deque<\/code>&nbsp;interface provides to implement stack:<\/p>\n\n\n\n<ul><li><code>push()<\/code>&nbsp;&#8211; adds an element at the beginning of deque<\/li><li><code>pop()<\/code>&nbsp;&#8211; removes an element from the beginning of deque<\/li><li><code>peek()<\/code>&nbsp;&#8211; returns an element from the beginning of deque<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"implementation-classes\">Implementation of Deque in ArrayDeque Class<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Deque;\nimport java.util.ArrayDeque;\n\nclass Main {\n\n    public static void main(String&#91;] args) {\n        \/\/ Creating Deque using the ArrayDeque class\n        Deque&lt;Integer&gt; numbers = new ArrayDeque&lt;&gt;();\n\n        \/\/ add elements to the Deque\n        numbers.offer(1);\n        numbers.offerLast(2);\n        numbers.offerFirst(3);\n        System.out.println(\"Deque: \" + numbers);\n\n        \/\/ Access elements of the Deque\n        int firstElement = numbers.peekFirst();\n        System.out.println(\"First Element: \" + firstElement);\n\n        int lastElement = numbers.peekLast();\n        System.out.println(\"Last Element: \" + lastElement);\n\n        \/\/ Remove elements from the Deque\n        int removedNumber1 = numbers.pollFirst();\n        System.out.println(\"Removed First Element: \" + removedNumber1);\n\n        int removedNumber2 = numbers.pollLast();\n        System.out.println(\"Removed Last Element: \" + removedNumber2);\n\n        System.out.println(\"Updated Deque: \" + numbers);\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>Deque: &#91;3, 1, 2]\nFirst Element: 3\nLast Element: 2\nRemoved First Element: 3\nRemoved Last Element: 2\nUpdated Deque: &#91;1]\n<\/samp><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about the Deque interface, how to use it, and its methods. The&nbsp;Deque&nbsp;interface of the Java collections framework provides the functionality of a double-ended queue. It extends the&nbsp;Queue&nbsp;interface. Working of Deque In a regular queue, elements are added from the rear and removed from the front. However, in a deque, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[291],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1265"}],"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=1265"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1265\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}