{"id":121191,"date":"2024-05-08T09:58:12","date_gmt":"2024-05-08T08:58:12","guid":{"rendered":"https:\/\/www.baeldung.com\/?p=180589"},"modified":"2024-05-08T09:58:12","modified_gmt":"2024-05-08T08:58:12","slug":"convert-a-queue-to-a-list","status":"publish","type":"post","link":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/2024\/05\/08\/convert-a-queue-to-a-list\/","title":{"rendered":"Convert a Queue to a List"},"content":{"rendered":"<p><img src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2021\/09\/Java-8-Featured-1024x536.png\" class=\"webfeedsFeaturedVisual wp-post-image\" alt=\"Contact Us Featured\" style=\"float: left; margin-right: 5px;\" decoding=\"async\" srcset=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2021\/09\/Java-8-Featured-1024x536.png 1024w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2021\/09\/Java-8-Featured-300x157.png 300w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2021\/09\/Java-8-Featured-768x402.png 768w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2021\/09\/Java-8-Featured-100x52.png 100w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2021\/09\/Java-8-Featured.png 1200w\" sizes=\"(max-width: 580px) 100vw, 580px\" \/><\/p>\n<h2 id=\"bd-overview\" data-id=\"overview\">1. Overview<\/h2>\n<div class=\"bd-anchor\" id=\"overview\"><\/div>\n<p>In this tutorial, we\u2019ll learn how to convert a <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/java-queue\"><em>Queue<\/em><\/a> object into a <em>List\u00a0<\/em>in Java.<\/p>\n<p>We&#8217;ll explain several popular ways of doing this along with their implementations, and we&#8217;ll end each section with a test case to test the respective implementation.<\/p>\n<h2 id=\"bd-ways-to-convert-queue-to-list\" data-id=\"ways-to-convert-queue-to-list\">2. Ways to Convert <em>Queue<\/em> to <em>List<\/em><\/h2>\n<div class=\"bd-anchor\" id=\"ways-to-convert-queue-to-list\"><\/div>\n<p>In this section, we\u2019ll cover different ways to convert the <em>Queue<\/em> to a <em>List<\/em> using standard Java classes and methods. We&#8217;re assuming a non-null queue for all examples.<\/p>\n<h3  data-id=\"1-using-the-arraylist-constructor\" id=\"bd-bd-1-using-the-arraylist-constructor\" data-id=\"bd-1-using-the-arraylist-constructor\">2.1. Using the <em>ArrayList<\/em>\u00a0Constructor<\/h3>\n<div class=\"bd-anchor\" id=\"bd-1-using-the-arraylist-constructor\"><\/div>\n<p>The <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/java-arraylist#Creation\"><em>ArrayList<\/em><\/a> constructor provides the easiest and most common way to convert a <em>Queue<\/em>\u00a0to an\u00a0<em>ArrayList<\/em>.<\/p>\n<p><strong>The idea is to pass the <em>Queue<\/em>\u00a0as a parameter to the\u00a0<em>ArrayList<\/em> constructor<\/strong>:<\/p>\n<pre><code class=\"language-java\">List&lt;String&gt; list = new ArrayList&lt;&gt;(queue);<\/code><\/pre>\n<p>The constructor <em>new ArrayList&lt;&gt;(queue) <\/em>efficiently inserts all of the <em>queue<\/em> elements into the new <em>ArrayList<\/em>.<\/p>\n<h3 id=\"bd-2-using-the-addall-method\" data-id=\"2-using-the-addall-method\">2.2. Using the <em>addAll()<\/em> method<\/h3>\n<div class=\"bd-anchor\" id=\"2-using-the-addall-method\"><\/div>\n<p>The\u00a0<a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/java-add-items-array-list#addall\"><em>addAll()<\/em><\/a>\u00a0method is another great option to consider if we want to convert a <i>Queue <\/i>to a <em>List<\/em>.<\/p>\n<p>As the name implies,\u00a0<strong>this method allows adding all elements in the specified collection to the <em>List<\/em><\/strong>.<\/p>\n<p>Now, let&#8217;s see the logic:<\/p>\n<pre><code class=\"language-java\">List&lt;String&gt; list = new ArrayList&lt;&gt;();\r\nlist.addAll(queue);<\/code><\/pre>\n<p>We create a new <em>List<\/em> named <em>list<\/em> and populate it with the elements of the <em>queue<\/em> using the <em>addAll()<\/em> method. The method then returns the resultant <em>list.<\/em><\/p>\n<h3  data-id=\"1-using-the-arraylist-constructor\" id=\"bd-bd-1-using-the-arraylist-constructor-1\" data-id=\"bd-1-using-the-arraylist-constructor-1\">2.3. Using the <em>LinkedList\u00a0<\/em>Constructor<\/h3>\n<div class=\"bd-anchor\" id=\"bd-1-using-the-arraylist-constructor-1\"><\/div>\n<p>The <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/java-linkedlist\"><em>LinkedList<\/em><\/a> constructor provides the most common and easiest way to convert a <em>Queue<\/em> to a <em>LinkedList<\/em>.<\/p>\n<p>Similar to the above example using the <em>ArrayList<\/em> constructor, <strong>we simply pass the <em>Queue<\/em> as a parameter to the\u00a0<em>LinkedList<\/em> constructor<\/strong>:<\/p>\n<pre><code class=\"language-java\">LinkedList&lt;String&gt; list = new LinkedList&lt;&gt;(queue);<\/code><\/pre>\n<p>The constructor <em>new LinkedList&lt;&gt;(queue) <\/em>efficiently converts the <em>queue<\/em> elements into the new <em>LinkedList<\/em>.<\/p>\n<h3 id=\"bd-4-using-stream-api\" data-id=\"4-using-stream-api\">2.4. Using Stream API<\/h3>\n<div class=\"bd-anchor\" id=\"4-using-stream-api\"><\/div>\n<p>Java 8 comes with a lot of new features that help in enhancing our code. Among these features, we find the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/java-8-streams\">Stream API<\/a>.<\/p>\n<p>Let\u2019s illustrate how to use the <em>stream<\/em> API to convert a <em>Queue<\/em> to a <em>List<\/em>:<\/p>\n<pre><code class=\"language-java\">List&lt;String&gt; list = queue.stream().collect(Collectors.toList());<\/code><\/pre>\n<p><strong>We convert the <em>queue<\/em> to a <em>List<\/em> using the <em>collect(Collectors.toList()) <\/em>operation, which collects the elements of the <em>stream<\/em> into a new list and returns it.<\/strong> This approach leverages a concise and functional programming style with streams to perform the conversion.<\/p>\n<h3 id=\"bd-5-using-guava\" data-id=\"5-using-guava\">2.5. Using Guava<\/h3>\n<div class=\"bd-anchor\" id=\"5-using-guava\"><\/div>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/guava-guide\">Guava<\/a> is a popular open-source library developed by Google that provides a wide range of utility classes and methods to simplify common programming tasks in Java.<\/p>\n<p>Let&#8217;s use Guava to convert <em>Queue<\/em> to <em>List<\/em>:<\/p>\n<pre><code class=\"language-java\">List&lt;String&gt; list = Lists.newArrayList(queue);<\/code><\/pre>\n<p><strong>Guava&#8217;s utility method <em>Lists.newArrayList(queue)<\/em> simplifies the process of creating a new list from the elements of a queue.<\/strong><\/p>\n<h2 id=\"bd-conclusion\" data-id=\"conclusion\">3. Conclusion<\/h2>\n<div class=\"bd-anchor\" id=\"conclusion\"><\/div>\n<p>In this article, we saw various ways to convert <em>Queue<\/em> to <em>List<\/em> in Java. Whether we prefer the simplicity of <em>LinkedList<\/em>, the versatility of <em>ArrayList<\/em>, the elegance of Java 8 streams, or the power of Guava, understanding these techniques empowers us to handle data seamlessly in our Java projects. We can experiment with these approaches to find the one best suited to our requirements and coding style.<\/p>\n<p>The source code of all these examples is available <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/github.com\/eugenp\/tutorials\/tree\/master\/core-java-modules\/core-java-collections-conversions-3\">over on GitHub<\/a>.<\/p>\n<p><Img align=\"left\" border=\"0\" height=\"1\" width=\"1\" alt=\"\" style=\"border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;\" hspace=\"0\" src=\"https:\/\/feeds.feedblitz.com\/~\/i\/896100500\/0\/baeldung\"><\/p>\n<div style=\"clear:both;padding-top:0.2em;\"><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/896100500\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/896100500\/baeldung,https%3A%2F%2Fwww.baeldung.com%2Fwp-content%2Fuploads%2F2021%2F09%2FJava-8-Featured-1024x536.png\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Post to X.com\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/896100500\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/x.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/896100500\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/896100500\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/java-convert-queue-list#respond\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>&#160;<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/java-convert-queue-list\/feed\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>&#160;<\/div>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2021\/09\/Java-8-Featured-1024x536.png\" class=\"webfeedsFeaturedVisual wp-post-image\" alt=\"Contact Us Featured\"><\/p>\n<p>Learn various ways to convert Queue to List in Java.<\/p>\n<div><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/896100500\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\"><\/a>\u00a0<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/896100500\/baeldung,https%3A%2F%2Fwww.baeldung.com%2Fwp-content%2Fuploads%2F2021%2F09%2FJava-8-Featured-1024x536.png\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\"><\/a>\u00a0<a title=\"Post to X.com\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/896100500\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/x.png\"><\/a>\u00a0<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/896100500\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\"><\/a>\u00a0<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/896100500\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\"><\/a>\u00a0<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/java-convert-queue-list#respond\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>\u00a0<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/java-convert-queue-list\/feed\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>\u00a0<\/div>\n","protected":false},"author":2098,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[22],"tags":[61,122,127,129,124,128,125,132,131,133,126,130,123,66,94,88,97,56,64,65,60,112,40,75,95,104,33,120,105,101,98,115,30,29,41,86,70,69,68,72,71,26,118,108,87,46,55,48,52,54,51,50,83,62,58,57,29691,109,35,59,63,85,79,82,96,80,27,81,114,44,42,43,45,38,39,110,117,100,111,116,73,89,90,92,91,93,84,78,37,102,34,36,77,67,74,99,113,119,28,121,32,47,49,53,103,31,76],"class_list":["post-121191","post","type-post","status-publish","format-standard","hentry","category-mobile","tag-airpods","tag-anime","tag-anime-characters","tag-anime-cosplay","tag-anime-edits","tag-anime-merchandise","tag-anime-movies","tag-anime-news","tag-anime-recommendations","tag-anime-reviews","tag-anime-series","tag-anime-streaming","tag-animes","tag-app-store","tag-app-store-samsung","tag-appgallery","tag-appgallery-oneplus","tag-apple","tag-apple-music","tag-apple-tv","tag-apple-watch","tag-bbc-sport","tag-best-mobile-games","tag-bixby","tag-bixby-xiaomi","tag-champions-league","tag-cyberpunk","tag-cyberpunk-2077","tag-fantasy-football","tag-fifa","tag-football","tag-formula-1","tag-fortnite","tag-free-fire","tag-free-mobile-games","tag-freebuds-pro","tag-galaxy-a52","tag-galaxy-note-20","tag-galaxy-s21","tag-galaxy-watch-4","tag-galaxy-z-fold-3","tag-game","tag-games","tag-golf","tag-harmonyos","tag-how-to-backup-iphone","tag-how-to-factory-reset-iphone","tag-how-to-reset-iphone","tag-how-to-restore-iphone","tag-how-to-unlock-iphone","tag-how-to-unlock-iphone-5","tag-how-to-unlock-iphone-6","tag-huawei","tag-ios","tag-ipad","tag-iphone","tag-java-queue","tag-live-soccer","tag-lol","tag-macbook","tag-macos","tag-mate-40-pro","tag-mi-11-lite","tag-mi-home-security-camera-basic-1080p","tag-mi-home-security-camera-basic-1080p-huawei","tag-mi-smart-band-6","tag-minecraft","tag-miui","tag-mlb-scores","tag-mobile-game-design","tag-mobile-game-development","tag-mobile-game-marketing","tag-mobile-game-monetization","tag-mobile-games","tag-mobile-gaming","tag-nba-scores","tag-nba-standings","tag-nfl","tag-nfl-scores","tag-nhl-scores","tag-one-ui","tag-oneplus","tag-oneplus-9-pro","tag-oneplus-buds-pro","tag-oneplus-nord-ce-5g","tag-oxygenos","tag-p40-pro-plus","tag-poco-x3-pro","tag-pokemon","tag-premier-league","tag-pubg","tag-pubg-mobile","tag-redmi-note-10-pro","tag-samsung","tag-samsung-pay","tag-soccer","tag-sports","tag-steam","tag-steeam","tag-top-10-anime","tag-valorant","tag-when-do-the-iphone-7-come-out","tag-when-does-the-iphone-7-come-out","tag-when-is-the-iphone-7-coming-out","tag-world-cup","tag-xbox-series-x","tag-xiaomi"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/wp-json\/wp\/v2\/posts\/121191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/wp-json\/wp\/v2\/users\/2098"}],"replies":[{"embeddable":true,"href":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/wp-json\/wp\/v2\/comments?post=121191"}],"version-history":[{"count":1,"href":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/wp-json\/wp\/v2\/posts\/121191\/revisions"}],"predecessor-version":[{"id":121221,"href":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/wp-json\/wp\/v2\/posts\/121191\/revisions\/121221"}],"wp:attachment":[{"href":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/wp-json\/wp\/v2\/media?parent=121191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/wp-json\/wp\/v2\/categories?post=121191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gamefootballmobileanimeiphone.com\/index.php\/wp-json\/wp\/v2\/tags?post=121191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}