{"id":1261,"date":"2022-02-26T07:05:25","date_gmt":"2022-02-26T07:05:25","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1261"},"modified":"2022-02-26T07:05:25","modified_gmt":"2022-02-26T07:05:25","slug":"queue-interface","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/26\/queue-interface\/","title":{"rendered":"Queue Interface"},"content":{"rendered":"\n<p>In this tutorial, we will learn about the Java Queue interface and its methods.<\/p>\n\n\n\n<p>The&nbsp;<code>Queue<\/code>&nbsp;interface of the Java collections framework provides the functionality of the queue data structure. It extends the&nbsp;<code>Collection<\/code>&nbsp;interface.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"classes\">Classes that Implement Queue<\/h2>\n\n\n\n<p>Since the&nbsp;<code>Queue<\/code>&nbsp;is an interface, we cannot provide the direct implementation of it.<\/p>\n\n\n\n<p>In order to use the functionalities of&nbsp;<code>Queue<\/code>, we need to use classes that implement it:<\/p>\n\n\n\n<ul><li>ArrayDeque<\/li><li>LinkedList<\/li><li>PriorityQueue<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/queue-interface.png\" alt=\"ArrayDeque, LinkedList and PriorityQueue implements the Queue interface in Java.\"\/><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Interfaces that extend Queue<\/h2>\n\n\n\n<p>The&nbsp;<code>Queue<\/code>&nbsp;interface is also extended by various subinterfaces:<\/p>\n\n\n\n<ul><li><code>Deque<\/code><\/li><li><code>BlockingQueue<\/code><\/li><li><code>BlockingDeque<\/code><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/queue-subinterfaces.png\" alt=\"Deque, BlockingQueue and BlockingDeque extends the the Queue interface.\"\/><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"working\">Working of Queue Data Structure<\/h2>\n\n\n\n<p>In queues, elements are stored and accessed in&nbsp;<strong>First In, First Out<\/strong>&nbsp;manner. That is, elements are&nbsp;<strong>added from the behind<\/strong>&nbsp;and&nbsp;<strong>removed from the front<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/queue-implementation.png\" alt=\"Working of queue data structure: first in first out.\"\/><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"use\">How to use Queue?<\/h2>\n\n\n\n<p>In Java, we must import&nbsp;<code>java.util.Queue<\/code>&nbsp;package in order to use&nbsp;<code>Queue<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ LinkedList implementation of Queue\nQueue&lt;String&gt; animal1 = new LinkedList&lt;&gt;();\n\n\/\/ Array implementation of Queue\nQueue&lt;String&gt; animal2 = new ArrayDeque&lt;&gt;();\n\n\/\/ Priority Queue implementation of Queue\nQueue&lt;String&gt; animal 3 = new PriorityQueue&lt;&gt;();\n<\/code><\/pre>\n\n\n\n<p>Here, we have created objects&nbsp;<var>animal1<\/var>,&nbsp;<var>animal2<\/var>&nbsp;and&nbsp;<var>animal3<\/var>&nbsp;of classes&nbsp;<code>LinkedList<\/code>,&nbsp;<code>ArrayDeque<\/code>&nbsp;and&nbsp;<code>PriorityQueue<\/code>&nbsp;respectively. These objects can use the functionalities of 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=\"methods\">Methods of Queue<\/h2>\n\n\n\n<p>The&nbsp;<code>Queue<\/code>&nbsp;interface includes all the methods of the&nbsp;<code>Collection<\/code>&nbsp;interface. It is because&nbsp;<code>Collection<\/code>&nbsp;is the super interface of&nbsp;<code>Queue<\/code>.<\/p>\n\n\n\n<p>Some of the commonly used methods of the&nbsp;<code>Queue<\/code>&nbsp;interface are:<\/p>\n\n\n\n<ul><li><strong>add()<\/strong>&nbsp;&#8211; Inserts the specified element into the queue. If the task is successful,&nbsp;<code>add()<\/code>&nbsp;returns&nbsp;<code>true<\/code>, if not it throws an exception.<\/li><li><strong>offer()<\/strong>&nbsp;&#8211; Inserts the specified element into the queue. If the task is successful,&nbsp;<code>offer()<\/code>&nbsp;returns&nbsp;<code>true<\/code>, if not it returns&nbsp;<code>false<\/code>.<\/li><li><strong>element()<\/strong>&nbsp;&#8211; Returns the head of the queue. Throws an exception if the queue is empty.<\/li><li><strong>peek()<\/strong>&nbsp;&#8211; Returns the head of the queue. Returns&nbsp;<code>null<\/code>&nbsp;if the queue is empty.<\/li><li><strong>remove()<\/strong>&nbsp;&#8211; Returns and removes the head of the queue. Throws an exception if the queue is empty.<\/li><li><strong>poll()<\/strong>&nbsp;&#8211; Returns and removes the head of the queue. Returns&nbsp;<code>null<\/code>&nbsp;if the queue is empty.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"implementation-classes\">Implementation of the Queue Interface<\/h2>\n\n\n\n<p><strong>1. Implementing the LinkedList Class<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Queue;\nimport java.util.LinkedList;\n\nclass Main {\n\n    public static void main(String&#91;] args) {\n        \/\/ Creating Queue using the LinkedList class\n        Queue&lt;Integer&gt; numbers = new LinkedList&lt;&gt;();\n\n        \/\/ offer elements to the Queue\n        numbers.offer(1);\n        numbers.offer(2);\n        numbers.offer(3);\n        System.out.println(\"Queue: \" + numbers);\n\n        \/\/ Access elements of the Queue\n        int accessedNumber = numbers.peek();\n        System.out.println(\"Accessed Element: \" + accessedNumber);\n\n        \/\/ Remove elements from the Queue\n        int removedNumber = numbers.poll();\n        System.out.println(\"Removed Element: \" + removedNumber);\n\n        System.out.println(\"Updated Queue: \" + 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>Queue: &#91;1, 2, 3]\nAccessed Element: 1\nRemoved Element: 1\nUpdated Queue: &#91;2, 3]\n<\/samp><\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>2. Implementing the PriorityQueue Class<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Queue;\nimport java.util.PriorityQueue;\n\nclass Main {\n\n    public static void main(String&#91;] args) {\n        \/\/ Creating Queue using the PriorityQueue class\n        Queue&lt;Integer&gt; numbers = new PriorityQueue&lt;&gt;();\n\n        \/\/ offer elements to the Queue\n        numbers.offer(5);\n        numbers.offer(1);\n        numbers.offer(2);\n        System.out.println(\"Queue: \" + numbers);\n\n        \/\/ Access elements of the Queue\n        int accessedNumber = numbers.peek();\n        System.out.println(\"Accessed Element: \" + accessedNumber);\n\n        \/\/ Remove elements from the Queue\n        int removedNumber = numbers.poll();\n        System.out.println(\"Removed Element: \" + removedNumber);\n\n        System.out.println(\"Updated Queue: \" + 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>Queue: &#91;1, 5, 2]\nAccessed Element: 1\nRemoved Element: 1\nUpdated Queue: &#91;2, 5]<\/samp><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about the Java Queue interface and its methods. The&nbsp;Queue&nbsp;interface of the Java collections framework provides the functionality of the queue data structure. It extends the&nbsp;Collection&nbsp;interface. Classes that Implement Queue Since the&nbsp;Queue&nbsp;is an interface, we cannot provide the direct implementation of it. In order to use the functionalities of&nbsp;Queue, we [&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\/1261"}],"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=1261"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1261\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}