{"id":648,"date":"2021-06-21T13:06:52","date_gmt":"2021-06-21T13:06:52","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2021\/06\/21\/queue-in-java\/"},"modified":"2021-06-21T13:06:52","modified_gmt":"2021-06-21T13:06:52","slug":"queue-in-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2021\/06\/21\/queue-in-java\/","title":{"rendered":"Queue in Java"},"content":{"rendered":"<p><html><body><\/p>\n<section class=\"tcb-post-content tcb-shortcode thrv_wrapper\" data-css=\"tve-u-16ec5d248bb\">\n<div class=\"tve_flt tcb-style-wrap\" id=\"tve_flt\">\n<div class=\"tve_shortcode_editor tar-main-content\" data-post-id=\"648\" id=\"tve_editor\">\n<div class=\"thrv_wrapper tve-toc tve-elem-scroll tve-toc-expandable tcb-local-vars-root\" data-animation=\"fade\" data-animation-speed=\"fast\" data-bullet-style='{\"0\":\"tve-u-1739a1b9ec9\",\"1\":\"tve-u-1739a1ba9d6\",\"2\":\"tve-u-1739a1bb53c\"}' data-columns=\"1\" data-columns-d=\"1\" data-css=\"tve-u-17a2ea9456b\" data-ct=\"toc-60672\" data-ct-name=\"Table of Contents 04\" data-element-name=\"Table of Contents\" data-headers=\"h1,h3,h5\" data-heading-style='{\"0\":\"tve-u-17a2ea94573\",\"1\":\"tve-u-17a2ea94575\",\"2\":\"tve-u-17a2ea94576\"}' data-highlight=\"section\" data-number-style='{\"0\":\"tve-u-17a2ea94574\",\"1\":\"tve-u-173dc88e09d\",\"2\":\"tve-u-173dc88e7b7\"}' data-numbering=\"basic\" data-state-default=\"expanded\" data-state-default-m=\"collapsed\" data-transition=\"slide\" style=\"\">\n<div class=\"thrive-colors-palette-config\" style=\"display: none !important\"><\/div>\n<div class=\"tve-toc-divider\" style=\"position: absolute; width: 0; height: 0; overflow: hidden;\">\n<div class=\"thrv_wrapper thrv-divider tve-vert-divider\" data-color-d=\"rgba(0, 0, 0, 0.24)\" data-css=\"tve-u-17a2ea9456d\" data-style=\"tve_sep-1\" data-style-d=\"tve_sep-3\" data-thickness-d=\"6\">\n<hr class=\"tve_sep tve_sep-3\" style=\"\"\/><\/div>\n<\/div>\n<p><svg class=\"toc-icons\" style=\"position: absolute; width: 0; height: 0; overflow: hidden;\" version=\"1.1\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\"><symbol data-id=\"icon-clone-outlined\" id=\"toc-bullet-0\" viewbox=\"0 0 512 512\"><path d=\"M464 0H144c-26.51 0-48 21.49-48 48v48H48c-26.51 0-48 21.49-48 48v320c0 26.51 21.49 48 48 48h320c26.51 0 48-21.49 48-48v-48h48c26.51 0 48-21.49 48-48V48c0-26.51-21.49-48-48-48zM362 464H54a6 6 0 0 1-6-6V150a6 6 0 0 1 6-6h42v224c0 26.51 21.49 48 48 48h224v42a6 6 0 0 1-6 6zm96-96H150a6 6 0 0 1-6-6V54a6 6 0 0 1 6-6h308a6 6 0 0 1 6 6v308a6 6 0 0 1-6 6z\"><\/path><\/symbol><symbol data-id=\"icon-caret-right-solid\" id=\"toc-bullet-1\" viewbox=\"0 0 192 512\">\n<path d=\"M0 384.662V127.338c0-17.818 21.543-26.741 34.142-14.142l128.662 128.662c7.81 7.81 7.81 20.474 0 28.284L34.142 398.804C21.543 411.404 0 402.48 0 384.662z\"><\/path>\n<\/symbol><symbol data-id=\"icon-caret-right-solid\" id=\"toc-bullet-2\" viewbox=\"0 0 192 512\">\n<path d=\"M0 384.662V127.338c0-17.818 21.543-26.741 34.142-14.142l128.662 128.662c7.81 7.81 7.81 20.474 0 28.284L34.142 398.804C21.543 411.404 0 402.48 0 384.662z\"><\/path>\n<\/symbol><\/svg><\/p>\n<div class=\"tve-content-box-background\" data-css=\"tve-u-17a2ea9456e\" style=\"\"><\/div>\n<div class=\"thrv_wrapper tve-toc-title show-icon reverse tcb-icon-display tve-no-dropzone tve-prevent-content-edit\" data-css=\"tve-u-17a2ea9456f\" style=\"\">\n<div class=\"tve-content-box-background\"><\/div>\n<div class=\"tve-cb\" style=\"\">\n<div class=\"tve-toc-title-icon\" data-icon-code=\"icon-close-solid\" style=\"\"><svg class=\"tcb-icon\" data-id=\"icon-close-solid\" data-name=\"\" viewbox=\"0 0 24 24\"><path d=\"M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z\"><\/path><\/svg><\/div>\n<div class=\"thrv_wrapper thrv_text_element tve_no_icons\">\n<div class=\"tcb-plain-text\" data-css=\"tve-u-17a2ea94570\" style=\"\">table of contents<\/div>\n<\/p><\/div>\n<\/div>\n<\/div>\n<div class=\"tve-cb tve-toc-content tve-prevent-content-edit\">\n<div class=\"thrv_wrapper tve-toc-list tcb-no-delete tcb-no-save tcb-no-clone tve-no-dropzone\" data-css=\"tve-u-17a2ea94571\" style=\"\">\n<div class=\"tve-content-box-background\" data-css=\"tve-u-17a2ea94572\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"tve_ct_content tve_clearfix\">\n<div class=\"ct_column\">\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17a2ea94575\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1624280357903\" jump-animation=\"smooth\">What is Queue interface and what are the characteristics of Queue in JAVA?<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17a2ea94575\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1624280357904\" jump-animation=\"smooth\">How to add element to a Queue?<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17a2ea94575\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1624280357905\" jump-animation=\"smooth\">How do we remove element from a Queue?<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17a2ea94575\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1624280357906\" jump-animation=\"smooth\">How do we find head element of a queue?<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17a2ea94575\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1624280357907\" jump-animation=\"smooth\">How to iterate all elements of a Queue?<\/a><\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv-divider tve-vert-divider\" data-color-d=\"rgba(0, 0, 0, 0.24)\" data-css=\"tve-u-17a2ea9456d\" data-style=\"tve_sep-1\" data-style-d=\"tve_sep-3\" data-thickness-d=\"6\">\n<hr class=\"tve_sep tve_sep-3\" style=\"\"\/><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h3 class=\"\">What is Queue interface and what are the characteristics of Queue in JAVA?<\/h3>\n<p>Queue in JAVA Collection framework is an interface, which extends Collection interface. It is part of java.util package. It is used to store objects to be processed in FIFO order (First In First Out). <\/p>\n<p>Since Queue is an interface, needs a concrete class for the declaration and the most common classes are the PriorityQueue and LinkedList.<\/p>\n<div class=\"code-block code-block-2\" style=\"margin: 8px auto; text-align: center; display: block; clear: both;\">\n<p><!-- horizontal responsive 2 --><br \/>\n<ins class=\"adsbygoogle\" data-ad-client=\"ca-pub-3231318496783163\" data-ad-format=\"auto\" data-ad-slot=\"2788468763\" data-full-width-responsive=\"true\" style=\"display:block\"><\/ins>\n<\/div>\n<h3 class=\"\">How to add element to a Queue?<\/h3>\n<p>There are 2 methods we can use to add element to a Queue, add() and offer(). The methods differ in how the behave if the Queue is full, so no more elements can be added. The add() method throws an exception in that case, whereas the offer() method just returns false.<\/p>\n<p><strong>Example \u2013<\/strong><\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17a2eaa2f5c\" style=\"\">\n<p data-css=\"tve-u-17a2eaa72ad\" style=\"\">import java.util.*;<\/p>\n<p>public class Main {<\/p>\n<p>public static void main(String[] args) {<br \/>Queue&lt;String&gt; queue = new LinkedList&lt;&gt;();<\/p>\n<p>queue.add(&#8220;element 1&#8221;);<br \/>queue.offer(&#8220;element 2&#8221;);<\/p>\n<p>}<br \/>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h3 class=\"\">How do we remove element from a Queue?<\/h3>\n<p>There are 2 methods which can be used to remove elements from a Queue. They are remove() and poll(). <\/p>\n<p>Both removes the first element of the Queue and returns the removed element. If the Queue is empty, remove() throws an exception while poll() returns null. <\/p>\n<p><strong>Example<\/strong><\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17a2eab1d3e\" style=\"\">\n<p data-css=\"tve-u-17a2eab665f\" style=\"\">public class Main {<\/p>\n<p>public static void main(String[] args) {<br \/>Queue&lt;String&gt; queue = new LinkedList&lt;&gt;();<\/p>\n<p>queue.add(&#8220;element 1&#8221;);<br \/>queue.offer(&#8220;element 2&#8221;);<br \/>String e1 = queue.remove();<br \/>String e2 = queue.poll();<\/p>\n<p>System.out.println(e1);<br \/>System.out.println(e2);<\/p>\n<p>}<br \/>}<\/p>\n<p><span data-css=\"tve-u-17a2eab7f55\" style=\"color: rgb(0, 255, 0);\">Output<\/p>\n<p>element 1<br \/>element 2<\/span><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>Explanation<\/strong><\/p>\n<p>The remove() method removes the first element which is \u2018element 1\u2019 and poll() method removes the second element which is \u2018element 2\u2019.<\/p>\n<p>Observe that FIFO rule is followed here.<\/p>\n<h3 class=\"\">How do we find head element of a queue?<\/h3>\n<p>We can find the head element of the Queue using element() or peek() methods, without removing the elements.<\/p>\n<p>\u00a0If the queue is non-empty, both returns the elements. Otherwise, element() throws exception and peek() returns null. <\/p>\n<p><strong>Example<\/strong><\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17a2eaca0be\" style=\"\">\n<p data-css=\"tve-u-17a2ead097e\" style=\"\">import java.util.*;<\/p>\n<p>public class Main {<\/p>\n<p>public static void main(String[] args) {<br \/>Queue&lt;String&gt; queue = new LinkedList&lt;&gt;();<\/p>\n<p>\/\/ Add elements to the queue<br \/>queue.add(&#8220;element 1&#8221;);<br \/>queue.offer(&#8220;element 2&#8221;);<br \/>\/\/ Find the head<br \/>String e1 = queue.element();<br \/>\/\/ Remove the head<br \/>queue.poll();<br \/>\/\/ Find the head<br \/>String e2 = queue.peek();<br \/>\/\/ Remove the head<br \/>queue.poll();<br \/>\/\/ Find the head for empty queue<br \/>String e3 = queue.peek();<\/p>\n<p>System.out.println(e1);<br \/>System.out.println(e2);<br \/>System.out.println(e3);<\/p>\n<p>}<br \/>}<\/p>\n<p><span data-css=\"tve-u-17a2ead2a04\" style=\"color: rgb(0, 255, 0);\">Output<\/p>\n<p>element 1<br \/>element 2<br \/>null<\/span><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h3 class=\"\">How to iterate all elements of a Queue?<\/h3>\n<p>We can use Iterator to iterate all elements of a Queue. One important thing to note is, if the implementation of Queue is LinkedList, after iterating, the elements will be returned based on insertion order (default behavior of LinkedList class) but if PriorityQueue implementation is used, the output will be in sorted order. <\/p>\n<p>Let\u2019s see the difference using 2 examples. <\/p>\n<p><strong>Example 1 \u2013 LinkedList<\/strong><\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17a2eadeadb\" style=\"\">\n<p data-css=\"tve-u-17a2eae3ffa\" style=\"\">import java.util.*;<\/p>\n<p>public class Main {<\/p>\n<p>public static void main(String[] args) {<br \/>Queue&lt;String&gt; queue = new LinkedList&lt;&gt;();<\/p>\n<p>\/\/ Add elements to the queue<br \/>queue.offer(&#8220;element 4&#8221;);<br \/>queue.offer(&#8220;element 5&#8221;);<br \/>queue.add(&#8220;element 1&#8221;);<br \/>queue.offer(&#8220;element 2&#8221;);<br \/>queue.offer(&#8220;element 3&#8221;);<\/p>\n<p>Iterator&lt;String&gt; itr = queue.iterator();<br \/>while(itr.hasNext())<br \/>{<br \/>System.out.println(itr.next());<br \/>}<\/p>\n<p>}<br \/>}<\/p>\n<p><span data-css=\"tve-u-17a2eae5941\" style=\"color: rgb(0, 255, 0);\">Output (Maintains the insertion order)<\/p>\n<p>element 4<br \/>element 5<br \/>element 1<br \/>element 2<br \/>element 3<\/span><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>Example 2 \u2013 PriorityQueue<\/strong><\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17a2eaec1cd\" style=\"\">\n<p data-css=\"tve-u-17a2eaf1176\" style=\"\">import java.util.*;<\/p>\n<p>public class Main {<\/p>\n<p>public static void main(String[] args) {<br \/>Queue&lt;String&gt; queue = new PriorityQueue&lt;&gt;();<\/p>\n<p>\/\/ Add elements to the queue<br \/>queue.offer(&#8220;element 4&#8221;);<br \/>queue.offer(&#8220;element 5&#8221;);<br \/>queue.add(&#8220;element 1&#8221;);<br \/>queue.offer(&#8220;element 2&#8221;);<br \/>queue.offer(&#8220;element 3&#8221;);<\/p>\n<p>Iterator&lt;String&gt; itr = queue.iterator();<br \/>while(itr.hasNext())<br \/>{<br \/>System.out.println(itr.next());<br \/>}<\/p>\n<p>}<br \/>}<\/p>\n<p><span data-css=\"tve-u-17a2eaf2bb9\" style=\"color: rgb(0, 255, 0);\">Output (Elements are returned in sorted way)<\/p>\n<p>element 1<br \/>element 2<br \/>element 4<br \/>element 5<br \/>element 3<\/span><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"tcb_flag\" style=\"display: none\"><\/div>\n<\/section>\n<p><\/body><\/html><\/p>\n","protected":false},"excerpt":{"rendered":"<p>table of contents What is Queue interface and what are the characteristics of Queue in JAVA? How to add element to a Queue? How do we remove element from a Queue? How do we find head element of a queue? How to iterate all elements of a Queue? What is Queue interface and what 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":[5],"tags":[],"class_list":["post-648","post","type-post","status-publish","format-standard","hentry","category-java","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=648"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/648\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}