<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Cast away with Java generics</title>
	<atom:link href="http://codemunchies.com/2009/10/cast-away-with-java-generics/feed/" rel="self" type="application/rss+xml" />
	<link>http://codemunchies.com/2009/10/cast-away-with-java-generics/</link>
	<description>Satisfy your cravings</description>
	<lastBuildDate>Tue, 20 Dec 2011 11:07:30 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Vladimir Dzhuvinov</title>
		<link>http://codemunchies.com/2009/10/cast-away-with-java-generics/comment-page-1/#comment-769</link>
		<dc:creator>Vladimir Dzhuvinov</dc:creator>
		<pubDate>Thu, 07 Jul 2011 13:15:26 +0000</pubDate>
		<guid isPermaLink="false">http://codemunchies.com/?p=175#comment-769</guid>
		<description>Since Java 1.5 there is actually a Class.cast() that does the same.</description>
		<content:encoded><![CDATA[<p>Since Java 1.5 there is actually a Class.cast() that does the same.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Harris</title>
		<link>http://codemunchies.com/2009/10/cast-away-with-java-generics/comment-page-1/#comment-272</link>
		<dc:creator>John Harris</dc:creator>
		<pubDate>Fri, 29 Jan 2010 18:54:06 +0000</pubDate>
		<guid isPermaLink="false">http://codemunchies.com/?p=175#comment-272</guid>
		<description>Surely this is missing the point of what the compiler is warning you about, your &#039;beautiful&#039; code is just moving the problem elsewhere.

Your original code has both an unchecked cast and an unchecked conversion. Seeing as neither are being checked you can write less code and get exactly the same bytecode by only coding the required cast i.e.:

Map&lt;String, Map&lt;String, Collection&gt;&gt; myUglyMap = (Map)webappSession.getAttribute(&quot;myUglyMap&quot;);

Which is the same length as your workaround without having to create an extra utility class and import it.

Now if you wanted to really address the problem you&#039;d need to define the parameterised type Map in a custom class, then perform a checked cast on the object from the session map.

Failing that you could iterate over the contents of the map (and nested map, and collection) checking their types. Without this you are doing an unchecked conversion and should rightly by warned by the compiler- if the cast passes you may still get class casts down the line as other code accesses the data in the Map.</description>
		<content:encoded><![CDATA[<p>Surely this is missing the point of what the compiler is warning you about, your &#8216;beautiful&#8217; code is just moving the problem elsewhere.</p>
<p>Your original code has both an unchecked cast and an unchecked conversion. Seeing as neither are being checked you can write less code and get exactly the same bytecode by only coding the required cast i.e.:</p>
<p>Map&lt;String, Map&lt;String, Collection&gt;&gt; myUglyMap = (Map)webappSession.getAttribute(&#8220;myUglyMap&#8221;);</p>
<p>Which is the same length as your workaround without having to create an extra utility class and import it.</p>
<p>Now if you wanted to really address the problem you&#8217;d need to define the parameterised type Map in a custom class, then perform a checked cast on the object from the session map.</p>
<p>Failing that you could iterate over the contents of the map (and nested map, and collection) checking their types. Without this you are doing an unchecked conversion and should rightly by warned by the compiler- if the cast passes you may still get class casts down the line as other code accesses the data in the Map.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nanda Firdausi</title>
		<link>http://codemunchies.com/2009/10/cast-away-with-java-generics/comment-page-1/#comment-270</link>
		<dc:creator>Nanda Firdausi</dc:creator>
		<pubDate>Wed, 06 Jan 2010 20:47:29 +0000</pubDate>
		<guid isPermaLink="false">http://codemunchies.com/?p=175#comment-270</guid>
		<description>Thanks for the nice code and please check my suggestion to make it better here: http://satukubik.com/2010/01/06/java-tips-using-generic-correctly/</description>
		<content:encoded><![CDATA[<p>Thanks for the nice code and please check my suggestion to make it better here: <a href="http://satukubik.com/2010/01/06/java-tips-using-generic-correctly/" rel="nofollow">http://satukubik.com/2010/01/06/java-tips-using-generic-correctly/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Java Tips: Using generic correctly &#124; satukubik</title>
		<link>http://codemunchies.com/2009/10/cast-away-with-java-generics/comment-page-1/#comment-269</link>
		<dc:creator>Java Tips: Using generic correctly &#124; satukubik</dc:creator>
		<pubDate>Wed, 06 Jan 2010 20:34:38 +0000</pubDate>
		<guid isPermaLink="false">http://codemunchies.com/?p=175#comment-269</guid>
		<description>[...] The last article I refer on article Java Tips: Iterate and cast is one example where we write a Java code using generic but not optimally. The code is as follow: [...]</description>
		<content:encoded><![CDATA[<p>[...] The last article I refer on article Java Tips: Iterate and cast is one example where we write a Java code using generic but not optimally. The code is as follow: [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Java Tips: Iterate and cast &#124; satukubik</title>
		<link>http://codemunchies.com/2009/10/cast-away-with-java-generics/comment-page-1/#comment-268</link>
		<dc:creator>Java Tips: Iterate and cast &#124; satukubik</dc:creator>
		<pubDate>Tue, 05 Jan 2010 10:11:13 +0000</pubDate>
		<guid isPermaLink="false">http://codemunchies.com/?p=175#comment-268</guid>
		<description>[...] came across this idea after reading an article Cast away with Java generics. The article suggests a nice approach to [...]</description>
		<content:encoded><![CDATA[<p>[...] came across this idea after reading an article Cast away with Java generics. The article suggests a nice approach to [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: r4 card</title>
		<link>http://codemunchies.com/2009/10/cast-away-with-java-generics/comment-page-1/#comment-87</link>
		<dc:creator>r4 card</dc:creator>
		<pubDate>Sat, 07 Nov 2009 13:06:45 +0000</pubDate>
		<guid isPermaLink="false">http://codemunchies.com/?p=175#comment-87</guid>
		<description>Hi..
Here is the code is given for the casting of Java code into Java Generics...
I will copied it for my future use..!!
Anyway Thanks for this Information....!!</description>
		<content:encoded><![CDATA[<p>Hi..<br />
Here is the code is given for the casting of Java code into Java Generics&#8230;<br />
I will copied it for my future use..!!<br />
Anyway Thanks for this Information&#8230;.!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JavaBlogging &#187; Beautiful casting gone bad</title>
		<link>http://codemunchies.com/2009/10/cast-away-with-java-generics/comment-page-1/#comment-55</link>
		<dc:creator>JavaBlogging &#187; Beautiful casting gone bad</dc:creator>
		<pubDate>Mon, 02 Nov 2009 12:00:57 +0000</pubDate>
		<guid isPermaLink="false">http://codemunchies.com/?p=175#comment-55</guid>
		<description>[...] I encountered a blog entry describing a &#8220;beautiful&#8221; way of making the code more readable by creating a gereric [...]</description>
		<content:encoded><![CDATA[<p>[...] I encountered a blog entry describing a &#8220;beautiful&#8221; way of making the code more readable by creating a gereric [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Ribeiro</title>
		<link>http://codemunchies.com/2009/10/cast-away-with-java-generics/comment-page-1/#comment-38</link>
		<dc:creator>Daniel Ribeiro</dc:creator>
		<pubDate>Thu, 29 Oct 2009 13:52:46 +0000</pubDate>
		<guid isPermaLink="false">http://codemunchies.com/?p=175#comment-38</guid>
		<description>@Jaran Nilsen
There are so many java open source projects around, that finding one with a feature you are looking for is quite hard. Google Guava is one of the most known solvers of of java annoyances, and still is widely unkwnown. But both Guava and Google collections don&#039;t solve some common tasks that Fluent Java does.

Interestingly enough, all of these annoyances are solved by Scala on its core language.</description>
		<content:encoded><![CDATA[<p>@Jaran Nilsen<br />
There are so many java open source projects around, that finding one with a feature you are looking for is quite hard. Google Guava is one of the most known solvers of of java annoyances, and still is widely unkwnown. But both Guava and Google collections don&#8217;t solve some common tasks that Fluent Java does.</p>
<p>Interestingly enough, all of these annoyances are solved by Scala on its core language.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aleksander Stensby</title>
		<link>http://codemunchies.com/2009/10/cast-away-with-java-generics/comment-page-1/#comment-35</link>
		<dc:creator>Aleksander Stensby</dc:creator>
		<pubDate>Wed, 28 Oct 2009 07:53:53 +0000</pubDate>
		<guid isPermaLink="false">http://codemunchies.com/?p=175#comment-35</guid>
		<description>@Daniel Ribeiro Hey, thanks for the link! Had FluentJava on my google code favorites but haven&#039;t had the chance to check it out yet - will definitely do so now:) Cheers!</description>
		<content:encoded><![CDATA[<p>@Daniel Ribeiro Hey, thanks for the link! Had FluentJava on my google code favorites but haven&#8217;t had the chance to check it out yet &#8211; will definitely do so now:) Cheers!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jaran Nilsen</title>
		<link>http://codemunchies.com/2009/10/cast-away-with-java-generics/comment-page-1/#comment-34</link>
		<dc:creator>Jaran Nilsen</dc:creator>
		<pubDate>Wed, 28 Oct 2009 07:39:23 +0000</pubDate>
		<guid isPermaLink="false">http://codemunchies.com/?p=175#comment-34</guid>
		<description>Thank you all for your feedback! :)

@ppow: I see your point if this was the only case, but you&#039;re tying your solution very tightly with this specific case. You would have to write a separate method for every attribute you wanted to fetch from the session, which I would consider a unecessary overhead in many cases. The point here was to have a generic method which would cause very little additional coding and make the code a little cleaner in the cases where you have no choice but to use this complex data structure :) As for type checking, aren&#039;t you simply moving the location of a possible ClassCastException?

@orlandocr: Thanks, we&#039;re well familiar with Google Collections and use it daily :) You should check out our &lt;a href=&quot;http://codemunchies.com/2009/10/beautiful-code-with-google-collections-guava-and-static-imports-part-1/&quot; rel=&quot;nofollow&quot;&gt;coverage of Google Collections and Guava&lt;/a&gt;, part 2 is coming up very soon. As for this case - where an object is fetched from session - Google Collection does not really solve the issue. Even if the data structure was constructed using the static method from Google Collections, I would still have to do the nasty cast when fetching it :)

@Daniel Ribeiro: Very cool! I was looking for other projects already providing this, unfortunately I did not discover yours.</description>
		<content:encoded><![CDATA[<p>Thank you all for your feedback! <img src='http://codemunchies.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>@ppow: I see your point if this was the only case, but you&#8217;re tying your solution very tightly with this specific case. You would have to write a separate method for every attribute you wanted to fetch from the session, which I would consider a unecessary overhead in many cases. The point here was to have a generic method which would cause very little additional coding and make the code a little cleaner in the cases where you have no choice but to use this complex data structure <img src='http://codemunchies.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  As for type checking, aren&#8217;t you simply moving the location of a possible ClassCastException?</p>
<p>@orlandocr: Thanks, we&#8217;re well familiar with Google Collections and use it daily <img src='http://codemunchies.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  You should check out our <a href="http://codemunchies.com/2009/10/beautiful-code-with-google-collections-guava-and-static-imports-part-1/" rel="nofollow">coverage of Google Collections and Guava</a>, part 2 is coming up very soon. As for this case &#8211; where an object is fetched from session &#8211; Google Collection does not really solve the issue. Even if the data structure was constructed using the static method from Google Collections, I would still have to do the nasty cast when fetching it <img src='http://codemunchies.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>@Daniel Ribeiro: Very cool! I was looking for other projects already providing this, unfortunately I did not discover yours.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

