{"id":1387,"date":"2010-12-02T22:39:31","date_gmt":"2010-12-02T21:39:31","guid":{"rendered":"http:\/\/www.gamlor.info\/wordpress\/?p=1387"},"modified":"2021-07-03T13:31:39","modified_gmt":"2021-07-03T12:31:39","slug":"a-use-cases-for-generic-type-inferences-in-java","status":"publish","type":"post","link":"https:\/\/www.gamlor.info\/wordpress\/2010\/12\/a-use-cases-for-generic-type-inferences-in-java\/","title":{"rendered":"Nicer Collection Instantiation In Java"},"content":{"rendered":"<p>In my <a href=\"?p=1372\">last post<\/a> I showed that you can write a \u2018cast\u2019-method with all kind of strange properties. You probably think that this is very stupid thing to do. This time I show a more useful application of the same behavior.<\/p>\n<div id=\"attachment_1405\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2010\/12\/collection-factories.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1405\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2010\/12\/collection-factories-300x211.png\" alt=\"Nice Factories For Collections\" title=\"collection-factories\" class=\"size-medium wp-image-1405\" width=\"300\" height=\"211\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2010\/12\/collection-factories-300x211.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2010\/12\/collection-factories.png 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1405\" class=\"wp-caption-text\">Nice Factories For Collections<\/p><\/div>\n<p>Think about all the places in your code where you create an instance of a collection. Lots of places, right? Anyway you always have to write down the type and generic arguments twice, which is very annoying. That\u2019s why in <a href=\"http:\/\/www.angelikalanger.com\/GenericsFAQ\/FAQSections\/TechnicalDetails.html#What is the diamond operator?\">Java 7 the diamond<\/a> operator will be introduced.<br \/>\n<script src=\"https:\/\/gist.github.com\/726103.js?file=annoying-usage.java\"><\/script><noscript><pre><code class=\"language-java java\">\/\/ Annoying double declaration of the generic arguments\nList&lt;String&gt; strings = new ArrayList&lt;String&gt;();\n\n\/\/ Even more annoying\nMap&lt;String,URI&gt; cityToWebsite = new HashMap&lt;String,URI&gt;();<\/code><\/pre><\/noscript><\/p>\n<p>Luckily you don\u2019t need to wait until Java 7 to improve the situation. This is place where generic type inference comes handy. You can create static factory methods for often used types such as lists, maps, sets etc. Then you can statically import those methods and use them instead of newing up the objects. The big advantage is that the generic arguments are inferred by the compiler and you don\u2019t need to specify them twice.  Here\u2019re the utility methods:<\/p>\n<script src=\"https:\/\/gist.github.com\/726103.js?file=CollectionUtils.java\"><\/script><noscript><pre><code class=\"language-java java\">public class CollectionUtils {\n    private CollectionUtils(){\n    }\n\n    public  static &lt;T&gt;  List&lt;T&gt; list(){\n        return new ArrayList&lt;T&gt;();\n    }\n\n    public  static &lt;T&gt;  List&lt;T&gt; list(T...elements){\n        return new ArrayList&lt;T&gt;(asList(elements));\n    }\n\n    public  static &lt;T&gt;  Set&lt;T&gt; set(){\n        return new HashSet&lt;T&gt;();\n    }\n    public  static &lt;T&gt;  Set&lt;T&gt; set(T...elements){\n        return new HashSet&lt;T&gt;(asList(elements));\n    }\n\n    public  static &lt;TKey,TValue&gt; Map&lt;TKey,TValue&gt; map(){\n        return new HashMap&lt;TKey,TValue&gt;();\n    }\n}<\/code><\/pre><\/noscript>\n<p>And here\u2019s the usage. Note that we\u2019re using static imports for the methods:<br \/>\n<script src=\"https:\/\/gist.github.com\/726103.js?file=UsingCollectionUtils.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">List&lt;String&gt; strings = list();\nList&lt;String&gt; moreStrings = list(&quot;Hi&quot;,&quot;There&quot;);\n\nSet&lt;String&gt; setOfStrings = set();\n\nMap&lt;String,URI&gt; cityToWebsite = map();<\/code><\/pre><\/noscript><\/p>\n<p>By the way this tricky also used in the the JDK libraries for the Collections.emptyList(), emptySet() and emptyMap() methods.<\/p>\n<p>That\u2019s it for now. Try it out and build your own set of static factory methods. It\u2019s a nice way to get rid of chatty and useless type declarations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my last post I showed that you can write a \u2018cast\u2019-method with all kind of strange properties. You probably think that this is very stupid thing to do. This&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[15],"tags":[202,295],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/1387"}],"collection":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/comments?post=1387"}],"version-history":[{"count":23,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/1387\/revisions"}],"predecessor-version":[{"id":3888,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/1387\/revisions\/3888"}],"wp:attachment":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/media?parent=1387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/categories?post=1387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/tags?post=1387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}