{"id":2715,"date":"2017-07-13T11:58:55","date_gmt":"2017-07-13T09:58:55","guid":{"rendered":"http:\/\/www.itersdesktop.com\/?p=2715"},"modified":"2017-07-13T12:08:34","modified_gmt":"2017-07-13T10:08:34","slug":"combine-two-lists-in-groovy","status":"publish","type":"post","link":"https:\/\/www.itersdesktop.com\/fr\/2017\/07\/13\/combine-two-lists-in-groovy\/","title":{"rendered":"Combine two lists in Groovy"},"content":{"rendered":"<p><em>Combination or concatenation of two or multiple lists are believed to appear quite frequently in programming. This article aims to show you how to union two lists in Groovy in different ways.<\/em><\/p>\n<h2>Combine lists with addAll<\/h2>\n<p>The\u00a0<span class=\"lang:default decode:true crayon-inline \">java.util.ArrayList.addAll(Collection&lt;? extends E&gt; c)<\/span>\u00a0 method appends all of the elements from the specified collection (the second list) to the end of the current list (the first list), in the order that they are returned by the specified collection&rsquo;s Iterator.<\/p>\n<pre class=\"lang:go decode:true\">def fruits = [\"Apple\", \"Banana\", \"Cherry\"]\r\ndef newFruits = [\"Pear\", \"Plum\", \"Strawberry\"]\r\nfruits.addAll(newFruits)\r\nprintln fruits<\/pre>\n<p>The output looks like the following<\/p>\n<pre class=\"lang:default decode:true \">[Apple, Banana, Cherry, Pear, Plum, Strawberry]<\/pre>\n<h2>Combine lists using the plus operator<\/h2>\n<p>Naturally we can think about the plus operator when combining two lists. The plus operator will return a new list containing all the elements of the two lists while and the\u00a0<span class=\"lang:default decode:true crayon-inline \">addAll<\/span>\u00a0 method appends the elements of the second list to the end of the first one.<\/p>\n<pre class=\"lang:default decode:true \">def fruits = [\"Apple\", \"Banana\", \"Cherry\"]\r\ndef newFruits = [\"Pear\", \"Plum\", \"Strawberry\"]\r\nprintln fruits + newFruits<\/pre>\n<p>Obviously, the output is the same as the one using addAll method.<\/p>\n<p>I hope you could be interested in this useful article. If you have any queries, feel free to drop me a message underneath.<\/p>","protected":false},"excerpt":{"rendered":"<p>Combination or concatenation of two or multiple lists are believed to appear quite frequently in programming. This article aims to show you how to union two lists in Groovy in&hellip; <\/p>\n","protected":false},"author":2,"featured_media":2717,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[595],"tags":[628,626,629,627,347],"class_list":["post-2715","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-groovy","tag-combination","tag-combine","tag-concat","tag-concatenation","tag-list"],"_links":{"self":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/2715","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/comments?post=2715"}],"version-history":[{"count":5,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/2715\/revisions"}],"predecessor-version":[{"id":2721,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/2715\/revisions\/2721"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/media\/2717"}],"wp:attachment":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/media?parent=2715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/categories?post=2715"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/tags?post=2715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}