<?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>My Own Private Thermopylae &#187; Collections</title>
	<atom:link href="http://thermopylae.net/blog/tag/collections/feed/" rel="self" type="application/rss+xml" />
	<link>http://thermopylae.net/blog</link>
	<description>Do I know what a rhetorical question is?</description>
	<lastBuildDate>Sat, 14 Mar 2009 18:53:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Effective Java Collections</title>
		<link>http://thermopylae.net/blog/2008/08/10/basic-tips-for-using-the-java-collections-framework/</link>
		<comments>http://thermopylae.net/blog/2008/08/10/basic-tips-for-using-the-java-collections-framework/#comments</comments>
		<pubDate>Sun, 10 Aug 2008 22:44:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Collections]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://thermopylae.net/blog/?p=40</guid>
		<description><![CDATA[The Collections Framework is an awesome way to represent may common types of data using the mathematical basis for sets. Sun has a great tutorial that explains what the framework is and how to use it. This is meant to be a simple guide (in the traditions of Effective Java) on how to use the [...]]]></description>
			<content:encoded><![CDATA[<p>The Collections Framework is an awesome way to represent may common types of data using the mathematical basis for sets. Sun has a great tutorial that explains what the framework is and how to use it. This is meant to be a simple guide (in the traditions of <a href="http://www.amazon.com/gp/product/0321356683?tag=myownprivatet-20">Effective Java</a>) on how to use the framework correctly.</p>
<p>To make it clear at a glance, the good and bad code samples are marked in <span class="good">green</span> and <span class="bad">red</span> respectively.</p>
<p><strong>Collections have an <code>isEmpty()</code> method</strong></p>
<p>The Collections framework goes out of its way to make the code reflect what you&#8217;re actually thinking. The proper way to check if a collection is empty is not to check if its size is zero. Use the isEmpty() method!</p>
<p><strong>Avoid returning <code>null</code> to mean an empty collection</strong></p>
<p>Leaving the possibility of returning a <code>null</code> instead of an empty collection makes your caller have to write extra boilerplate code to deal with that case. Most of the time an empty collection has the same meaning that a <code>null</code> would. Trying to use null to represent an error in whatever was supposed to generate the collection bypasses Java&#8217;s preferred way for handling errors: Exceptions.</p>
<p><strong>Create an empty collection using <code>Collections.empty***()</code> methods</strong></p>
<p>If you need an empty collection that&#8217;s guaranteed to stay empty (for example in your test code) use the Collections utility class to create them:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">List</span> myEmptyList <span style="color: #339933;">=</span> <span style="color: #003399;">Collections</span>.<span style="color: #006633;">emptyList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Set</span> myEmptySet <span style="color: #339933;">=</span> <span style="color: #003399;">Collections</span>.<span style="color: #006633;">emptySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Map</span> myEmptyMap <span style="color: #339933;">=</span> <span style="color: #003399;">Collections</span>.<span style="color: #006633;">emptyMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There are also constants for <code>List.EMPTY_LIST</code>, <code>Set.EMPTY_SET</code>, <code>Map.EMPTY_MAP</code>, but they&#8217;re best avoided since they do not support generics.</p>
<p><strong>Iterate through collections using the foreach form when possible</strong></p>
<p>You want the code to match your thought process as much as possible. Sometimes you can&#8217;t use the new foreach syntax introduced in Java 5 because you need access to the loop counter but in most cases the best way to iterate through a collection looks like this:</p>
<pre class="good" lang="java">List foo;
for(String bar : foo) {
    System.out.println(bar);
}</pre>
<p>The same foreach loop also works for arrays, with the exact same syntax.</p>
<p><strong>Choosing which collection type to use</strong></p>
<p>When you create a new data structure, don&#8217;t think of whether it&#8217;s a hash table, tree, whatever. Think about what the relationship of what you&#8217;re storing is. Is it a Collection, a Set, a Map, or a List?</p>
<p>If you&#8217;re not clear on what the difference is between a Collection, Map, Set, and a List check out the Javadocs (<a href="http://java.sun.com/j2se/1.5.0/docs/guide/collections/reference.html">Sun&#8217;s Cliff&#8217;s notes</a>). Start with what the requirements are for the data structure. Does it need to be ordered? Does it need to be unique? etc. Those will tell you which interface you need to use, and then you can decide what the underlying implementation should be. What if you use the wrong type of map/set/list and performance sucks? Well, then changing that implementation involves one line of code. And no one else needs to know or worry about it.</p>
<p><strong>The left side is always an interface!</strong></p>
<p>Like the title says, what you&#8217;re assigning to should almost never be an implementation. As a corollary, you should never return an implementation instead of an interface.</p>
<p>Instead of:</p>
<pre class="bad" lang="java">ArrayList foo = new ArrayList();
HashMap bar = new LinkedHashMap();

public TreeMap foo(){
...
}</pre>
<p>do this:</p>
<pre class="good" lang="java">List foo = new ArrayList();
Map bar = new LinkedHashMap();

public SortedMap foo(){
...
}</pre>
<p>There might be a few exceptions to this rule, but they&#8217;re VERY rare. If you find yourself in one of those, stop and think really hard about whether you do actually need to break it. Better yet, ask the developer next to you about what she thinks.</p>
<p><strong>If you&#8217;re explicitly casting, chances are something is wrong. Use generics.</strong></p>
<p>Try to use generics on your collections. Knowing if foo is a list of Integers or Strings will make your client code much easier to read and will protect you from a lot of mistakes. Much more importantly, it will make it significantly easier for the next person who reads the code to understand what&#8217;s going on. If you find that you need a collection that needs to store multiple types of objects, then you should ask yourself whether you really have the right data structure and interface. Almost always the need to do that is a sign of a bad OO design.</p>
]]></content:encoded>
			<wfw:commentRss>http://thermopylae.net/blog/2008/08/10/basic-tips-for-using-the-java-collections-framework/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

