{"id":26835,"date":"2024-03-16T07:25:22","date_gmt":"2024-03-16T07:25:22","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/"},"modified":"2024-03-22T09:33:22","modified_gmt":"2024-03-22T09:33:22","slug":"in-depth-explanation-of-java-data-structure-queue","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/","title":{"rendered":"In-depth explanation of Java data structure &#8211; Queue."},"content":{"rendered":"<p>A queue is a type of data structure that follows the first in, first out (FIFO) principle, allowing elements to be inserted at one end and removed at the other. In a queue, the end where elements are inserted is called the rear, and the end where elements are removed is called the front.<\/p>\n<p>The Queue interface in Java is a subinterface of the Collection interface, which defines methods for operating on queues. Common implementations include LinkedList and ArrayDeque.<\/p>\n<p>Here are the commonly used methods in the Queue interface:<\/p>\n<ol>\n<li>enqueue(E e): Insert element e into the end of the queue.<\/li>\n<li>dequeue(): Remove and return the element at the front of the queue.<\/li>\n<li>peek(): retrieves the element at the front of the queue without removing it.<\/li>\n<li>isEmpty() function is used to check if the queue is empty.<\/li>\n<li>size(): obtain the number of elements in the queue.<\/li>\n<\/ol>\n<p>LinkedList and ArrayDeque are commonly used queue implementation classes in Java. Both of them implement the Queue interface, allowing direct use of the methods defined in the Queue interface.<\/p>\n<ol>\n<li>LinkedList is a data structure that implements a doubly linked list and can be used as a queue. Its enqueue(E e) method inserts an element at the end of the list, while the dequeue() method removes and returns the first element of the list.<\/li>\n<\/ol>\n<p>Sample code:<\/p>\n<pre class=\"post-pre\"><code>Queue&lt;Integer&gt; queue = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">LinkedList<\/span>&lt;&gt;();\r\n\r\nqueue.enqueue(<span class=\"hljs-number\">1<\/span>); <span class=\"hljs-comment\">\/\/ \u961f\u5217\u4e2d\u63d2\u5165\u5143\u7d201<\/span>\r\nqueue.enqueue(<span class=\"hljs-number\">2<\/span>); <span class=\"hljs-comment\">\/\/ \u961f\u5217\u4e2d\u63d2\u5165\u5143\u7d202<\/span>\r\n\r\nSystem.out.println(queue.peek()); <span class=\"hljs-comment\">\/\/ \u8f93\u51fa\u961f\u5934\u5143\u7d20<\/span>\r\n\r\n<span class=\"hljs-type\">int<\/span> <span class=\"hljs-variable\">num<\/span> <span class=\"hljs-operator\">=<\/span> queue.dequeue(); <span class=\"hljs-comment\">\/\/ \u5220\u9664\u961f\u5934\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u8be5\u5143\u7d20<\/span>\r\nSystem.out.println(num); <span class=\"hljs-comment\">\/\/ \u8f93\u51fa\u5220\u9664\u7684\u5143\u7d20<\/span>\r\n\r\nSystem.out.println(queue.isEmpty()); <span class=\"hljs-comment\">\/\/ \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a<\/span>\r\nSystem.out.println(queue.size()); <span class=\"hljs-comment\">\/\/ \u8f93\u51fa\u961f\u5217\u4e2d\u5143\u7d20\u7684\u4e2a\u6570<\/span>\r\n<\/code><\/pre>\n<ol>\n<li>ArrayDeque is a class that implements a double-ended queue, and can also serve as an implementation of a queue. Its enqueue(E e) method inserts an element at the end of the double-ended queue, and the dequeue() method removes the first element of the double-ended queue and returns it.<\/li>\n<\/ol>\n<p>Sample code:<\/p>\n<pre class=\"post-pre\"><code>Queue&lt;Integer&gt; queue = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">ArrayDeque<\/span>&lt;&gt;();\r\n\r\nqueue.enqueue(<span class=\"hljs-number\">1<\/span>); <span class=\"hljs-comment\">\/\/ \u961f\u5217\u4e2d\u63d2\u5165\u5143\u7d201<\/span>\r\nqueue.enqueue(<span class=\"hljs-number\">2<\/span>); <span class=\"hljs-comment\">\/\/ \u961f\u5217\u4e2d\u63d2\u5165\u5143\u7d202<\/span>\r\n\r\nSystem.out.println(queue.peek()); <span class=\"hljs-comment\">\/\/ \u8f93\u51fa\u961f\u5934\u5143\u7d20<\/span>\r\n\r\n<span class=\"hljs-type\">int<\/span> <span class=\"hljs-variable\">num<\/span> <span class=\"hljs-operator\">=<\/span> queue.dequeue(); <span class=\"hljs-comment\">\/\/ \u5220\u9664\u961f\u5934\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u8be5\u5143\u7d20<\/span>\r\nSystem.out.println(num); <span class=\"hljs-comment\">\/\/ \u8f93\u51fa\u5220\u9664\u7684\u5143\u7d20<\/span>\r\n\r\nSystem.out.println(queue.isEmpty()); <span class=\"hljs-comment\">\/\/ \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a<\/span>\r\nSystem.out.println(queue.size()); <span class=\"hljs-comment\">\/\/ \u8f93\u51fa\u961f\u5217\u4e2d\u5143\u7d20\u7684\u4e2a\u6570<\/span>\r\n<\/code><\/pre>\n<p>The application scenarios of queues are very extensive, such as implementing message queues and thread pools, where queue data structures can be used.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A queue is a type of data structure that follows the first in, first out (FIFO) principle, allowing elements to be inserted at one end and removed at the other. In a queue, the end where elements are inserted is called the rear, and the end where elements are removed is called the front. The [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-26835","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>In-depth explanation of Java data structure - Queue. - Blog - Silicon Cloud<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"In-depth explanation of Java data structure - Queue.\" \/>\n<meta property=\"og:description\" content=\"A queue is a type of data structure that follows the first in, first out (FIFO) principle, allowing elements to be inserted at one end and removed at the other. In a queue, the end where elements are inserted is called the rear, and the end where elements are removed is called the front. The [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-16T07:25:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-22T09:33:22+00:00\" \/>\n<meta name=\"author\" content=\"Isabella Edwards\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Isabella Edwards\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/\"},\"author\":{\"name\":\"Isabella Edwards\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/5579144e23c225c8188167f3e3f888dd\"},\"headline\":\"In-depth explanation of Java data structure &#8211; Queue.\",\"datePublished\":\"2024-03-16T07:25:22+00:00\",\"dateModified\":\"2024-03-22T09:33:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/\"},\"wordCount\":304,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/\",\"name\":\"In-depth explanation of Java data structure - Queue. - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-16T07:25:22+00:00\",\"dateModified\":\"2024-03-22T09:33:22+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"In-depth explanation of Java data structure &#8211; Queue.\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/5579144e23c225c8188167f3e3f888dd\",\"name\":\"Isabella Edwards\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d4d4dec47f553ac7961d9fa4cc9bdcdcf5b7ce5106594330b6d25c5694fdbaec?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d4d4dec47f553ac7961d9fa4cc9bdcdcf5b7ce5106594330b6d25c5694fdbaec?s=96&d=mm&r=g\",\"caption\":\"Isabella Edwards\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/isabellaedwards\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"In-depth explanation of Java data structure - Queue. - Blog - Silicon Cloud","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/","og_locale":"en_US","og_type":"article","og_title":"In-depth explanation of Java data structure - Queue.","og_description":"A queue is a type of data structure that follows the first in, first out (FIFO) principle, allowing elements to be inserted at one end and removed at the other. In a queue, the end where elements are inserted is called the rear, and the end where elements are removed is called the front. The [&hellip;]","og_url":"https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-16T07:25:22+00:00","article_modified_time":"2024-03-22T09:33:22+00:00","author":"Isabella Edwards","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Isabella Edwards","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/"},"author":{"name":"Isabella Edwards","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/5579144e23c225c8188167f3e3f888dd"},"headline":"In-depth explanation of Java data structure &#8211; Queue.","datePublished":"2024-03-16T07:25:22+00:00","dateModified":"2024-03-22T09:33:22+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/"},"wordCount":304,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/","url":"https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/","name":"In-depth explanation of Java data structure - Queue. - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-16T07:25:22+00:00","dateModified":"2024-03-22T09:33:22+00:00","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/in-depth-explanation-of-java-data-structure-queue\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"In-depth explanation of Java data structure &#8211; Queue."}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/5579144e23c225c8188167f3e3f888dd","name":"Isabella Edwards","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d4d4dec47f553ac7961d9fa4cc9bdcdcf5b7ce5106594330b6d25c5694fdbaec?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d4d4dec47f553ac7961d9fa4cc9bdcdcf5b7ce5106594330b6d25c5694fdbaec?s=96&d=mm&r=g","caption":"Isabella Edwards"},"url":"https:\/\/www.silicloud.com\/blog\/author\/isabellaedwards\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/26835","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=26835"}],"version-history":[{"count":1,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/26835\/revisions"}],"predecessor-version":[{"id":61026,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/26835\/revisions\/61026"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=26835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=26835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=26835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}