<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Just Java &#187; Generics</title>
	<atom:link href="http://blog.cedarsoft.com/category/java/generics/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.cedarsoft.com</link>
	<description>Johannes Schneider (cedarsoft GmbH) about Java and related stuff</description>
	<lastBuildDate>Wed, 25 Aug 2010 09:46:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Generics and Wildcards: Part 1</title>
		<link>http://blog.cedarsoft.com/2010/01/wildcards-and-collections-part-1/</link>
		<comments>http://blog.cedarsoft.com/2010/01/wildcards-and-collections-part-1/#comments</comments>
		<pubDate>Sat, 02 Jan 2010 13:34:33 +0000</pubDate>
		<dc:creator>johannes</dc:creator>
				<category><![CDATA[Generics]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.cedarsoft.com/?p=75</guid>
		<description><![CDATA[Wildcards have been introduced to Java some years ago (in JDK 1.5). And they are easy to understand &#8211; as long as no collections are involved. Then everything becomes a little bit more complex&#8230;
This (and the following) blog posts will try to show in simple steps how to handle wildcards in collections correctly.
Collections + Generics ]]></description>
			<content:encoded><![CDATA[<p>Wildcards have been introduced to Java some years ago (in JDK 1.5). And they are easy to understand &#8211; as long as no collections are involved. Then everything becomes a little bit more complex&#8230;</p>
<p>This (and the following) blog posts will try to show in simple steps how to handle wildcards in collections correctly.</p>
<h2>Collections + Generics = non intuitional</h2>
<p>Every Java developer knows that an <em>Integer</em> is also a <em>Number</em> since <em>Integer extends Number</em>. So every time someone expects a <em>Number</em> (e.g. as method or constructor parameter), I may call the method with a parameter of type <em>Integer</em> (or <em>Double</em>, or&#8230;).</p>
<blockquote><p>Every time I say <em>Number</em>, I literally mean:<br />
An object of type <em>Number</em> or of any other class extending <em>Number</em>.</p></blockquote>
<p>And every Java developer is used to understand it this way (ok, there might be some that don&#8217;t, but hey, those won&#8217;t read that blog post&#8230;).</p>
<p>When it comes to collections, most of the Java developers unconsciously transfer that understanding. At first glance it seems to be the same thing&#8230;<br />
<em>Set extends Collection</em>: So when a method parameter is declared as <em>Collection</em>, it might be called using a <em>Set</em> (or <em>List</em>, or&#8230;).</p>
<h3>Collections and their content</h3>
<p>But collections don&#8217;t just have a type. They also contain objects. And those objects also have a type. And that is the point where the trouble begin. The first step seems to be very obvious.</p>
<p><em>List&lt;Integer&gt;</em> is just a <em>List</em> containing <em>Integer</em>s. <em>List#add</em> accepts an <em>Integer</em>, <em>List#get</em> returns an <em>Integer</em>&#8230;</p>
<p>And of course the same can be said about a List of Numbers:</p>
<p><em>List&lt;Number&gt;</em> is just a <em>List</em> containing <em>Numbers</em>. <em>List#add</em> accepts a <em>Number</em>, <em>List#get</em> returns a <em>Number</em>&#8230;</p>
<p>The difference between a <em>List&lt;Integer&gt;</em> and List&lt;Number&gt; is, that the former may contain just Integers (and sub types) while the later might contain also Doubles, Longs and so on.</p>
<h3>Collections: The difference</h3>
<p>Following the usual experience, one might think, that that assignment works:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">List<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> integerList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
List<span style="color: #339933;">&lt;</span>Number<span style="color: #339933;">&gt;</span> numberList <span style="color: #339933;">=</span> integerList<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//!!!! Does not work !!!!!</span></div></td></tr></tbody></table></div>
<p>This assignment shouldn&#8217;t be problematic concerning read access. Every read access should return one or more Integers. And since Integer extends Number everything seems to be ok.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">List<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> integerList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
List<span style="color: #339933;">&lt;</span>Number<span style="color: #339933;">&gt;</span> numberList <span style="color: #339933;">=</span> integerList<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//!!!! Does not work !!!!!</span><br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Anumber+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Number</span></a> n <span style="color: #339933;">=</span> numberList.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//works</span></div></td></tr></tbody></table></div>
<p>But what&#8217;s about write access?</p>
<p>The compiler allows you to add any object of type Number to be added to numberList (List&lt;Number&gt;). But of course it must not be possible to add an object of type Number to List&lt;Integer&gt;.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">List<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> integerList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
List<span style="color: #339933;">&lt;</span>Number<span style="color: #339933;">&gt;</span> numberList <span style="color: #339933;">=</span> integerList<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//!!!! Does not work, because of next line!</span><br />
numberList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adouble+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Double</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2.0</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//We add a double to List&lt;Integer&gt;!!!!</span></div></td></tr></tbody></table></div>
<h3>Fruits&#8230;</h3>
<p>A list of fruits may contain all kinds of fruits. Apples, strawberries and so on. A list of apples may only contain apples. Therefore a list of apples must never be cast to List&lt;Fruit&gt;.</p>
<h1>The solution (at first non intuitional, too)</h1>
<p>Some smart minds at Sun discovered that problem. And they also introduced the solution: Wildcards. Just accept wildcards as necessary &#8211; because they are.</p>
<p>First the example:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">List<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> integerList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
List<span style="color: #339933;">&lt;?</span> <span style="color: #000000; font-weight: bold;">extends</span> Number<span style="color: #339933;">&gt;</span> numberList <span style="color: #339933;">=</span> integerList<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Works!</span><br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Anumber+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Number</span></a> n <span style="color: #339933;">=</span> numberList.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//works, since every Integer is also a Number</span><br />
numberList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adouble+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Double</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2.0</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Compiler error!!!</span></div></td></tr></tbody></table></div>
<p>We may assign List&lt;Integer&gt; to List&lt;? extends Number&gt; since Integer extends Number.</p>
<blockquote><p>The declaration List&lt;? extends Number&gt; may be read as: A list of an unknown subtype of Number.</p></blockquote>
<p>Of course read access is possible. We now that the containing elements must be of type Number. But adding is not possible since the exact type of the list is unknown.</p>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://blog.cedarsoft.com/2008/01/why-you-should-only-return-collections-with-bounded-wildcards/" rel="bookmark" class="crp_title">Why you should only return Collections with bounded wildcards</a></li><li><a href="http://blog.cedarsoft.com/2008/01/generics-and-collections-done-right-1-foolproof-step/" rel="bookmark" class="crp_title">Generics and Collections done right (1 foolproof step)&#8230;</a></li><li><a href="http://blog.cedarsoft.com/2010/08/java-surprise-settersgetters-and-collections/" rel="bookmark" class="crp_title">Java Surprise: Setters/Getters and Collections</a></li><li><a href="http://blog.cedarsoft.com/2010/04/javafx-transparency-and-linux/" rel="bookmark" class="crp_title">JavaFX: Transparency and Linux</a></li><li><a href="http://blog.cedarsoft.com/2010/04/javafx-1-3-template-for-custom-controls/" rel="bookmark" class="crp_title">JavaFX 1.3: Template for custom controls</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://blog.cedarsoft.com/2010/01/wildcards-and-collections-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why you should only return Collections with bounded wildcards</title>
		<link>http://blog.cedarsoft.com/2008/01/why-you-should-only-return-collections-with-bounded-wildcards/</link>
		<comments>http://blog.cedarsoft.com/2008/01/why-you-should-only-return-collections-with-bounded-wildcards/#comments</comments>
		<pubDate>Sat, 12 Jan 2008 22:37:01 +0000</pubDate>
		<dc:creator>johannes</dc:creator>
				<category><![CDATA[Generics]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.cedarsoft.com/?p=20</guid>
		<description><![CDATA[My post yesterday did not receive much compliance.
Many agreed, that collections with wildcards are usefull as method parameters, most deny their value as type for return values
That&#8217;s why I have created a small example that displays the problems not using wildcards. Unfortunately these problems are not very obvious and appear late. Besides that, most of ]]></description>
			<content:encoded><![CDATA[<p>My <a href="http://blog.cedarsoft.eu/2008/01/11/generics-and-collections-done-right-1-foolproof-step/">post</a> yesterday did not receive much compliance.</p>
<p>Many agreed, that collections with wildcards are usefull as method parameters, most deny their value as type for return values</p>
<p>That&#8217;s why I have created a small example that displays the problems not using wildcards. Unfortunately these problems are not very obvious and appear late. Besides that, most of the time the caller has to puzzle with it and maybe you will never hear about it.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> TightList<span style="color: #339933;">&lt;</span>E<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; List<span style="color: #339933;">&lt;</span>E<span style="color: #339933;">&gt;</span> getIt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">final</span> TightList<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> integerList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TightList<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> getIt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">final</span> TightList<span style="color: #339933;">&lt;</span>Double<span style="color: #339933;">&gt;</span> doubleList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TightList<span style="color: #339933;">&lt;</span>Double<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Double<span style="color: #339933;">&gt;</span> getIt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Double<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
<br />
TightList<span style="color: #339933;">&lt;</span>Number<span style="color: #339933;">&gt;</span> wrapping <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TightList<span style="color: #339933;">&lt;</span>Number<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Number<span style="color: #339933;">&gt;</span> getIt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> someFancyCondition <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> doubleList.<span style="color: #006633;">getIt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Compiler error</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> integerList.<span style="color: #006633;">getIt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Compiler error</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>I think it is quite obvious what happens here.</p>
<p>We have a simple interface called &#8220;TightList&#8221; that returns a collection without bounded wildcard. I created three implementations. The first two of them are straight forward &#8211; no problems occur here.</p>
<p>But the third one becomes critical. <strong>Wrapping the other TightLists is not possible</strong>.</p>
<h3>Conclusion</h3>
<p><strong>There is at least one case, where returning collections without wildcard, is problematic.<br />
Please, oblige the users of your API and add those wildcards (at least when the collection is unmodifiable &#8211; and it really should be).</strong></p>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://blog.cedarsoft.com/2010/01/wildcards-and-collections-part-1/" rel="bookmark" class="crp_title">Generics and Wildcards: Part 1</a></li><li><a href="http://blog.cedarsoft.com/2008/01/generics-and-collections-done-right-1-foolproof-step/" rel="bookmark" class="crp_title">Generics and Collections done right (1 foolproof step)&#8230;</a></li><li><a href="http://blog.cedarsoft.com/2010/08/java-surprise-settersgetters-and-collections/" rel="bookmark" class="crp_title">Java Surprise: Setters/Getters and Collections</a></li><li><a href="http://blog.cedarsoft.com/2010/04/javafx-transparency-and-linux/" rel="bookmark" class="crp_title">JavaFX: Transparency and Linux</a></li><li><a href="http://blog.cedarsoft.com/2010/04/javafx-bind-bug-or-am-i-just-stupid/" rel="bookmark" class="crp_title">[JavaFX] Bind bug? Or am I just stupid?</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://blog.cedarsoft.com/2008/01/why-you-should-only-return-collections-with-bounded-wildcards/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generics and Collections done right (1 foolproof step)&#8230;</title>
		<link>http://blog.cedarsoft.com/2008/01/generics-and-collections-done-right-1-foolproof-step/</link>
		<comments>http://blog.cedarsoft.com/2008/01/generics-and-collections-done-right-1-foolproof-step/#comments</comments>
		<pubDate>Fri, 11 Jan 2008 22:36:48 +0000</pubDate>
		<dc:creator>johannes</dc:creator>
				<category><![CDATA[Generics]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.cedarsoft.com/?p=19</guid>
		<description><![CDATA[Generics are surely the biggest change (considered from developers point of view) in Java since a very long time. And they are very powerful and comfortable to use. Learning a new API is *much* easier now.And it also saves a bunch of key presses when using an IDE (who needs Closures when your IDE creates ]]></description>
			<content:encoded><![CDATA[<p>Generics are surely the biggest change (considered from developers point of view) in Java since a very long time. And they are very powerful and comfortable to use. Learning a new API is *much* easier now.And it also saves a bunch of key presses when using an IDE (who needs Closures when your IDE creates generified anonymous classes?).</p>
<h3>But unfortunately very often Generics are used wrongly.</h3>
<p>Most of the time most of us are simply too stupid to understand Generics. Do you know anybody that completely understands the concepts? Me neither.</p>
<p>While Generics offer very big improvements for the user in conjunction with Collections, it seems to be hard to set them up correctly (for the design  of an API). And there exist several famous failures&#8230;</p>
<h2>Famous Failures</h2>
<h3>Google Guice</h3>
<p>Although Bob Lee is a brilliant guy (much smarter than I ever will be) there exist several flaws within his great framework Guice.I have created a patch that solves the biggest mistake. Unfortunately the patch has been accepted only partially and just fixed the worst issues.</p>
<h3>Glazed List</h3>
<p>Uuuh&#8230; At least there exists a generified version. But that is the only positive thing that could be said about.Main problem: At the moment it is impossible to register an EventListener to an EventList that has a bounded wildcard (okay you can add some fancy casts to &#8220;solve&#8221; this issue&#8230;).</p>
<p>Assumed you have something like <span class="textit">EventList</span> (EventList extendes java.util.List). You won&#8217;t be able to register an EvenListener&#8230;</p>
<h2>So now is the time to improve the world!</h2>
<p>Now is the time to improve your API 100 times with just one simple, foolproof step.</p>
<p>You don&#8217;t have to fully understand the concepts of Generics (I am far away from it).  Just follow this advice and most of the cases will work just as expected:</p>
<h2>Never, never, never use any type of Collection *without* (bounded) wildcards in a method signature.</h2>
<p>There only exist very few cases where a collection without a wildcard is needed. And most of these cases should be described accurately as hacks.</p>
<h3>Don&#8217;t return a collection without a (bounded) wildcard.</h3>
<h4>Wrong:</h4>
<p><span class="textit">public List getCustomers();</span></p>
<h4>Instead use:</h4>
<p><span class="textit">public List< <span class="textbf">? extends</span> Customer> getCustomers();</p>
<h3>And please, please never accept a collection without a (bounded) wildcard.</h3>
<h4>Wrong:</h4>
<p><span class="textit">public void addThings( Collection things);</span></p>
<h4>Instead use:</h4>
<p><span class="textit">public void addThings( Collection< <span class="textbf">? extends</span> Thing> things);</p>
<h2>Why is it so important?</h2>
<p>It is *very* important because it is not possible to widen a collection later:<br />
<span class="textit">List doubleList = new ArrayList();</span><br />
<span class="textit">List numberList = doubleList; //Compiler error!!!</span></p>
<p><span class="textit">numberList.add(new Integer(4)); //as numberList==doubleList</span><br />
A list of Doubles is a completely other thing than a list of Numbers. It is often forgotten that Collection classes also offer *write* access. And the compiler must avoid the addition of objects to  collections with the wrong type.</p>
<h2>The killer argument</h2>
<p>You *only* have to know the exact type of a collection, if you try to change that collection (e.g. add an element).</p>
<p>If you return an unmodifiable collection (and you really should in most cases), nobody will ever be able to add an element to  that collection. So nobody will ever have to know the *exact* type of that collection.</p>
<p>Return a collection generified with a bounded wildcard and nobody will ever *think* about touching the list!</p>
<p>Same goes for method parameters. In the most cases only the content of the collection is used &#8211; no write access is needed! So it isn&#8217;t necessary to know the exact type of the collection. Offer the caller a little luxury and go without the additional but completely useless bit of information.</p>
<h4>Conclusion:</h4>
<p><em><strong>As long as there is no need for changing a collection use wildcards.</strong></em></p>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://blog.cedarsoft.com/2008/01/why-you-should-only-return-collections-with-bounded-wildcards/" rel="bookmark" class="crp_title">Why you should only return Collections with bounded wildcards</a></li><li><a href="http://blog.cedarsoft.com/2010/01/wildcards-and-collections-part-1/" rel="bookmark" class="crp_title">Generics and Wildcards: Part 1</a></li><li><a href="http://blog.cedarsoft.com/2010/08/java-surprise-settersgetters-and-collections/" rel="bookmark" class="crp_title">Java Surprise: Setters/Getters and Collections</a></li><li><a href="http://blog.cedarsoft.com/2010/01/dont-miss-the-revolution/" rel="bookmark" class="crp_title">Don&#8217;t miss the revolution&#8230;</a></li><li><a href="http://blog.cedarsoft.com/2009/12/whats-wrong-with-xstream-and-similar-tools/" rel="bookmark" class="crp_title">What&#8217;s wrong with XStream and similar tools?</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://blog.cedarsoft.com/2008/01/generics-and-collections-done-right-1-foolproof-step/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
