<?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; Collections</title>
	<atom:link href="http://blog.cedarsoft.com/tag/collections/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.cedarsoft.com</link>
	<description>A blog about Java and (related) Open Source tools...</description>
	<lastBuildDate>Wed, 25 Jan 2012 12:02:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Java Surprise: Setters/Getters and Collections</title>
		<link>http://blog.cedarsoft.com/2010/08/java-surprise-settersgetters-and-collections/</link>
		<comments>http://blog.cedarsoft.com/2010/08/java-surprise-settersgetters-and-collections/#comments</comments>
		<pubDate>Thu, 05 Aug 2010 09:27:08 +0000</pubDate>
		<dc:creator>Johannes Schneider</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Collections]]></category>
		<category><![CDATA[puzzle]]></category>
		<category><![CDATA[surprise]]></category>

		<guid isPermaLink="false">http://blog.cedarsoft.com/?p=385</guid>
		<description><![CDATA[Inspired by the Java Killers series, I wanted to post a problem I stumbled across some years ago:
Imagine a bean containing a field.
class Car {
private final List tires = new ArrayList();

public void setTires( List tires ) {
this.tires.clear();
this.tires.addAll( tires );
}

public void addTire( Tire tires ) {
this.tires.add( tires );
}

public List getTires() {
return Collections.unmodifiableList( tires );
}
}
What is the [...]]]></description>
			<content:encoded><![CDATA[<p>Inspired by the Java Killers series, I wanted to post a problem I stumbled across some years ago:</p>
<p>Imagine a bean containing a field.</p>
<pre class="brush:java">class Car {
private final List tires = new ArrayList();

public void setTires( List tires ) {
this.tires.clear();
this.tires.addAll( tires );
}

public void addTire( Tire tires ) {
this.tires.add( tires );
}

public List getTires() {
return Collections.unmodifiableList( tires );
}
}</pre>
<p>What is the output of that code?</p>
<pre class="brush:java">Car car = new Car();
car.addTire( new Tire() );
car.addTire( new Tire() );

List carTires = car.getTires();
System.out.println( "before: " + carTires.size() );
car.setTires( carTires );
System.out.println( "after1: " + car.getTires().size() );
System.out.println( "after2: " + carTires.size() );</pre>
<p>The first part is easy:</p>
<pre>before: 2</pre>
<p>But the rest?</p>
<p><span id="more-386"></span></p>
<pre>before: 2
after1: 0
after2: 0</pre>
<p>We have managed to empty the collection accidentally &#8211; even if it is unmodifiable.<br />
The problem lies in the setter and how <em>Collections.unmodifiableList()</em> is implemented:</p>
<p>Collections.unmodifiableList returns a list that is backed by the original list. Every change to the original collections is reflected.<br />
Therefore the &#8220;clear&#8221; call empties also the unmodifiable list.</p>
<pre class="brush:java">public void setTires( List tires ) {
this.tires.clear(); //the list is cleared! But since the method parameter is a view of this.tires, it is emptied, too.
this.tires.addAll( tires ); //since tires is empty now, addAll doesn't do anything...
}</pre>
<p>So be careful with collections. They might change a really surprising moments&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cedarsoft.com/2010/08/java-surprise-settersgetters-and-collections/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Google releases some nice code&#8230;</title>
		<link>http://blog.cedarsoft.com/2007/09/google-releases-some-nice-code/</link>
		<comments>http://blog.cedarsoft.com/2007/09/google-releases-some-nice-code/#comments</comments>
		<pubDate>Thu, 13 Sep 2007 22:34:44 +0000</pubDate>
		<dc:creator>Johannes Schneider</dc:creator>
				<category><![CDATA[Guice]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Collections]]></category>
		<category><![CDATA[DI]]></category>

		<guid isPermaLink="false">http://blog.cedarsoft.com/?p=11</guid>
		<description><![CDATA[Google Guice is a really great Dependency Injection Framework and worth a look. Especially the performance and &#8211; much more important &#8211; the configuration using Java code in a very  intuitive form are great features.
They have now released another project that can be very, very helpful. It is called Google Collections and provides some [...]]]></description>
			<content:encoded><![CDATA[<p>Google Guice is a really great Dependency Injection Framework and worth a look. Especially the performance and &#8211; much more important &#8211; the configuration using Java code in a very  intuitive form are great features.</p>
<p>They have now released another <a href="http://code.google.com/p/google-collections/" target="_blank">project</a> that can be very, very helpful. It is called <a href="http://code.google.com/p/google-collections/" target="_blank">Google Collections</a> and provides some small but helpful classes.</p>
<p>A series describing the details can be found here:<br />
<a href="http://publicobject.com/2007/09/coding-in-small-with-google-collections_08.html" target="_blank">http://publicobject.com/2007/09/coding-in-small-with-google-collections_08.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cedarsoft.com/2007/09/google-releases-some-nice-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

