{"id":2,"date":"2005-04-10T22:51:15","date_gmt":"2005-04-10T12:51:15","guid":{"rendered":"http:\/\/www.thunderguy.com\/plusplus\/20050410\/use-generic-collection-interfaces-as-parameters"},"modified":"2005-04-10T22:51:15","modified_gmt":"2005-04-10T12:51:15","slug":"use-generic-collection-interfaces-as-parameters","status":"publish","type":"post","link":"https:\/\/thunderguy.com\/semicolon\/2005\/04\/10\/use-generic-collection-interfaces-as-parameters\/","title":{"rendered":"Use generic collection interfaces as parameters"},"content":{"rendered":"<p>Functions that take collection parameters should take the generic parameter. E.g. don&#8217;t do this:<\/p>\n<pre class=\"code\">\r\n<code class=\"bad-code\">public void process(ArrayList list);<\/code>\r\n<\/pre>\n<p>do this instead:<\/p>\n<pre class=\"code\">\r\n<code>public void process(List list);<\/code>\r\n<\/pre>\n<p>This avoids putting unnecessary requirements on the parameter. In particular, you can do these:<\/p>\n<pre class=\"code\">\r\n<code>thing.processList(Collections.EMPTY_LIST);<\/code>\r\n<\/pre>\n<pre class=\"code\">\r\n<code>thing.processList(Collections.singletonList(item));<\/code>\r\n<\/pre>\n<pre class=\"code\">\r\n<code>LinkedList linkedList = getLinkedList();\r\nthing.processList(linkedList);<\/code>\r\n<\/pre>\n<p>instead of having to do these:<\/p>\n<pre class=\"code\">\r\n<code class=\"bad-code\">thing.processList(new ArrayList());<\/code>\r\n<\/pre>\n<pre class=\"code\">\r\n<code class=\"bad-code\">ArrayList list = new ArrayList(1);\r\nlist.add(item);\r\nthing.processList(list);<\/code>\r\n<\/pre>\n<pre class=\"code\">\r\n<code class=\"bad-code\">LinkedList linkedList = getLinkedList();\r\nthing.processList(new ArrayList(linkedList));<\/code>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Functions that take collection parameters should take the generic parameter. E.g. don&#8217;t do this: public void process(ArrayList list); do this instead: public void process(List list); This avoids putting unnecessary requirements on the parameter. In particular, you can do these: thing.processList(Collections.EMPTY_LIST); thing.processList(Collections.singletonList(item)); LinkedList linkedList = getLinkedList(); thing.processList(linkedList); instead of having to do these: thing.processList(new ArrayList()); ArrayList [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[4],"class_list":["post-2","post","type-post","status-publish","format-standard","hentry","tag-java"],"_links":{"self":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts\/2","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/comments?post=2"}],"version-history":[{"count":0,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts\/2\/revisions"}],"wp:attachment":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/media?parent=2"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/categories?post=2"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/tags?post=2"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}