<?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: Java gotcha: anArray.hashCode isn&#8217;t deep</title>
	<atom:link href="http://www.databasesandlife.com/java-gotcha-anarrayhashcode-isnt-deep/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.databasesandlife.com/java-gotcha-anarrayhashcode-isnt-deep/</link>
	<description>Adrian Smith's blog</description>
	<lastBuildDate>Mon, 06 Feb 2012 05:20:25 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: John</title>
		<link>http://www.databasesandlife.com/java-gotcha-anarrayhashcode-isnt-deep/comment-page-1/#comment-5411</link>
		<dc:creator>John</dc:creator>
		<pubDate>Thu, 09 Jul 2009 17:11:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.databasesandlife.com/java-gotcha-anarrayhashcode-isnt-deep/#comment-5411</guid>
		<description>Warning, do not use the code recommended above:

&lt;code&gt;
Map&lt;Integer, byte[]&gt; map = new HashMap&lt;Integer, byte[]&gt;();
map.put(Arrays.hashCode(anArray), anArray);
Collection&lt;byte[]&gt; uniqueByteArrays = map.values();
&lt;/code&gt;

It is true that two identical arrays will hash to the same integer value, but the inverse is &lt;em&gt;not&lt;/em&gt; true. It is possible that two different arrays could hash to the same integer value. There are only so many integers, while the number of different integer arrays is almost unlimited.</description>
		<content:encoded><![CDATA[<p>Warning, do not use the code recommended above:</p>
<p><code><br />
Map&lt;Integer, byte[]&gt; map = new HashMap&lt;Integer, byte[]&gt;();<br />
map.put(Arrays.hashCode(anArray), anArray);<br />
Collection&lt;byte[]&gt; uniqueByteArrays = map.values();<br />
</code></p>
<p>It is true that two identical arrays will hash to the same integer value, but the inverse is <em>not</em> true. It is possible that two different arrays could hash to the same integer value. There are only so many integers, while the number of different integer arrays is almost unlimited.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Lawrey</title>
		<link>http://www.databasesandlife.com/java-gotcha-anarrayhashcode-isnt-deep/comment-page-1/#comment-429</link>
		<dc:creator>Peter Lawrey</dc:creator>
		<pubDate>Mon, 12 May 2008 11:24:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.databasesandlife.com/java-gotcha-anarrayhashcode-isnt-deep/#comment-429</guid>
		<description>Unfortunately, arrays inherit their implementation of hashCode(), equals() and toString() fro Object.  There is no Array class type that all array inherit from only an Array &amp; Arrays helper classes.
This means the hashCode for an array is based on it internal object number, equals is only true for the same object and toString() prints the internal class name and the internal id... Not very useful IMHO.</description>
		<content:encoded><![CDATA[<p>Unfortunately, arrays inherit their implementation of hashCode(), equals() and toString() fro Object.  There is no Array class type that all array inherit from only an Array &amp; Arrays helper classes.<br />
This means the hashCode for an array is based on it internal object number, equals is only true for the same object and toString() prints the internal class name and the internal id&#8230; Not very useful IMHO.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vesa Karvonen</title>
		<link>http://www.databasesandlife.com/java-gotcha-anarrayhashcode-isnt-deep/comment-page-1/#comment-325</link>
		<dc:creator>Vesa Karvonen</dc:creator>
		<pubDate>Thu, 27 Mar 2008 14:40:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.databasesandlife.com/java-gotcha-anarrayhashcode-isnt-deep/#comment-325</guid>
		<description>The (default) hash of mutable objects should only depend on the identity of the object.  If this isn&#039;t the case, then mutating the object changes the hash.  This, in turn, breaks the assumptions of data structures (read: hash tables) based on hashing.  Let&#039;s assume that the hash of an array would depend on the elements stored in the array.  Consider a program that first inserts an array into a hash table and then mutates the array.  After the mutation, the hash of the array has changed.  Trying to lookup the array will now fail.

Of course, one fundamental problem with Java is that it doesn&#039;t provide a comprehensive set of immutable data types.  This leads to many kinds of problems.  One well known problem is the need to make copies of data structures before returning them to avoid clients to break class invariants via mutation.  Contrast with a sane imperative language like Standard ML that provides comprehensive support for both mutable (ref cells and arrays) and immutable data types (vectors, tuples, records, variant types, numeric types, ...).  In Standard ML one can entirely avoid using mutable data types in interfaces except when the data structures are supposed to be mutated by the clients.  Consequently, there is basically never need to make copies of data structures just to be safe.</description>
		<content:encoded><![CDATA[<p>The (default) hash of mutable objects should only depend on the identity of the object.  If this isn&#8217;t the case, then mutating the object changes the hash.  This, in turn, breaks the assumptions of data structures (read: hash tables) based on hashing.  Let&#8217;s assume that the hash of an array would depend on the elements stored in the array.  Consider a program that first inserts an array into a hash table and then mutates the array.  After the mutation, the hash of the array has changed.  Trying to lookup the array will now fail.</p>
<p>Of course, one fundamental problem with Java is that it doesn&#8217;t provide a comprehensive set of immutable data types.  This leads to many kinds of problems.  One well known problem is the need to make copies of data structures before returning them to avoid clients to break class invariants via mutation.  Contrast with a sane imperative language like Standard ML that provides comprehensive support for both mutable (ref cells and arrays) and immutable data types (vectors, tuples, records, variant types, numeric types, &#8230;).  In Standard ML one can entirely avoid using mutable data types in interfaces except when the data structures are supposed to be mutated by the clients.  Consequently, there is basically never need to make copies of data structures just to be safe.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

