{"id":133,"date":"2013-07-09T10:43:00","date_gmt":"2013-07-09T10:43:00","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=133"},"modified":"2013-07-09T11:35:57","modified_gmt":"2013-07-09T11:35:57","slug":"why-collections-addall-runs-faster-than-arrays-aslist","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/why-collections-addall-runs-faster-than-arrays-aslist\/","title":{"rendered":"Why Collections.addAll() runs faster than Arrays.asList()"},"content":{"rendered":"<p>Consider the Following Example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n Collection&lt;Integer&gt; coll = new ArrayList&lt;Integer&gt;();\r\n\r\n coll.addAll(Arrays.asList(1,2,3,4,5));\r\n<\/pre>\n<p>In the above addAll the addition takes place in following steps as Below<br \/>\n1.varargs+autoboxing creates Integer[]<br \/>\n2.Arrays.asList creates a List<Integer> backed by the array<br \/>\n3.addAll iterates over a Collection<Integer> using Iterator<Integer><\/p>\n<p>Now consider the Below Code<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n Collection&lt;Integer&gt; coll = new ArrayList&lt;Integer&gt;();\r\n\r\n coll.addAll(coll, 1,2,3,4,5);\r\n<\/pre>\n<p>1.varargs+autoboxing creates Integer[]<br \/>\n2.addAll iterates over an array (instead of an Iterable<Integer>)<\/p>\n<p>We can see now that b) may be faster because:<\/p>\n<p>Arrays.asList call is skipped, i.e. no intermediary List is created.<br \/>\nSince the elements are given in an array, iterating over them may be faster than using Iterator.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Consider the Following Example Collection&lt;Integer&gt; coll = new ArrayList&lt;Integer&gt;(); coll.addAll(Arrays.asList(1,2,3,4,5)); In the above addAll the addition takes place in following steps as Below 1.varargs+autoboxing creates Integer[] 2.Arrays.asList creates a List backed by the array 3.addAll iterates over a Collection using Iterator Now consider the Below Code Collection&lt;Integer&gt; coll = new ArrayList&lt;Integer&gt;(); coll.addAll(coll, 1,2,3,4,5); 1.varargs+autoboxing creates&hellip; <a href=\"https:\/\/codethataint.com\/blog\/why-collections-addall-runs-faster-than-arrays-aslist\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-133","post","type-post","status-publish","format-standard","hentry","category-generics-and-collections"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=133"}],"version-history":[{"count":3,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/133\/revisions"}],"predecessor-version":[{"id":141,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/133\/revisions\/141"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}