<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:series="http://unfoldingneurons.com/" version="2.0">
<channel>
	<title>Comments for mgm technology blog</title>
	
	<link>http://blog.mgm-tp.com</link>
	<description>We discuss software innovation</description>
	<lastBuildDate>Thu, 16 May 2013 06:52:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
	<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/MgmTechBlogComments" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="mgmtechblogcomments" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">MgmTechBlogComments</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Comment on Tuning Garbage Collection for Mission-Critical Java Applications by Nizar</title>
		<link>http://blog.mgm-tp.com/2013/03/garbage-collection-tuning/comment-page-1/#comment-4582</link>
		<dc:creator>Nizar</dc:creator>
		<pubDate>Thu, 16 May 2013 06:52:26 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mgm-tp.com/?p=1525#comment-4582</guid>
		<description>Good, very good article. 
I am interested about tool (s) you have used to mesure all Heap and GC indicators.</description>
		<content:encoded><![CDATA[<p>Good, very good article.<br />
I am interested about tool (s) you have used to mesure all Heap and GC indicators.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Must-Know URL Hash Techniques for AJAX Applications by Subhasis</title>
		<link>http://blog.mgm-tp.com/2011/10/must-know-url-hashtechniques-for-ajax-applications/comment-page-1/#comment-4515</link>
		<dc:creator>Subhasis</dc:creator>
		<pubDate>Sun, 05 May 2013 05:59:31 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mgm-tp.com/?p=1006#comment-4515</guid>
		<description>Awesome article .. thanks for the detailed explanation!</description>
		<content:encoded><![CDATA[<p>Awesome article .. thanks for the detailed explanation!</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Must-Know URL Hash Techniques for AJAX Applications by Anup Yadav</title>
		<link>http://blog.mgm-tp.com/2011/10/must-know-url-hashtechniques-for-ajax-applications/comment-page-1/#comment-4495</link>
		<dc:creator>Anup Yadav</dc:creator>
		<pubDate>Fri, 03 May 2013 12:27:55 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mgm-tp.com/?p=1006#comment-4495</guid>
		<description>Thanks for the great explanation!!!</description>
		<content:encoded><![CDATA[<p>Thanks for the great explanation!!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Portlets with XForms in Liferay by christian</title>
		<link>http://blog.mgm-tp.com/2011/01/rethinking-web-forms-xforms-part3/comment-page-1/#comment-4287</link>
		<dc:creator>christian</dc:creator>
		<pubDate>Tue, 23 Apr 2013 09:03:05 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mgm-tp.com/?p=824#comment-4287</guid>
		<description>@Lukas : I remember we had to escape the JavaScript and CSS Ressources somehow, because there were some parsing issues. As far as I remeber, we used CDATA segments</description>
		<content:encoded><![CDATA[<p>@Lukas : I remember we had to escape the JavaScript and CSS Ressources somehow, because there were some parsing issues. As far as I remeber, we used CDATA segments</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Designing and Implementing our Camel-based mgm Cosmo Router by Michael Frieß</title>
		<link>http://blog.mgm-tp.com/2013/04/camel-router-part1/comment-page-1/#comment-4246</link>
		<dc:creator>Michael Frieß</dc:creator>
		<pubDate>Fri, 19 Apr 2013 16:09:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mgm-tp.com/?p=1513#comment-4246</guid>
		<description>Thank you for your comment. We appreciate the positive feedback and the reference on the Camel website!

I'm looking forward to a noop option in future Camel releases which could further reduce the need for custom logic or "workarounds" like the multicast EIP.

The new SQL component options in the upcoming Camel 2.11 sound indeed like an interesting alternative to the Timer endpoints and the &lt;code&gt;SqlExecutor#selectNewTransfers&lt;/code&gt; method as described in the article. The new option "useIterator" looks like an appropriate alternative to the "&lt;code&gt;&lt;split/&gt;&lt;/code&gt;" usage, too.

The named parameters are interesting as well. Wouldn't it be even nicer if their resolving process would consider the Exchange's properties, too?
Here is a simple (untestet) example what I mean:

[code lang="xml" wraplines="false"]
&lt;route&gt;
  &lt;setProperty name="prop1"&gt;&lt;simple&gt;${TransferId}&lt;/simple&gt;&lt;/setProperty&gt;
  &lt;!-- ${body}: contains the SOAP message --&gt;
  &lt;to uri="sql:UPDATE TRANSFER SET RESPONSE_XML = # WHERE ID = :prop1"/&gt;
&lt;/route&gt;
[/code]</description>
		<content:encoded><![CDATA[<p>Thank you for your comment. We appreciate the positive feedback and the reference on the Camel website!</p>
<p>I&#8217;m looking forward to a noop option in future Camel releases which could further reduce the need for custom logic or &#8220;workarounds&#8221; like the multicast EIP.</p>
<p>The new SQL component options in the upcoming Camel 2.11 sound indeed like an interesting alternative to the Timer endpoints and the <code>SqlExecutor#selectNewTransfers</code> method as described in the article. The new option &#8220;useIterator&#8221; looks like an appropriate alternative to the &#8220;<code>&lt;split/&gt;</code>&#8221; usage, too.</p>
<p>The named parameters are interesting as well. Wouldn&#8217;t it be even nicer if their resolving process would consider the Exchange&#8217;s properties, too?<br />
Here is a simple (untestet) example what I mean:</p>
<pre class="brush: xml; wrap-lines: false;">
&lt;route&gt;
  &lt;setProperty name=&quot;prop1&quot;&gt;&lt;simple&gt;${TransferId}&lt;/simple&gt;&lt;/setProperty&gt;
  &lt;!-- ${body}: contains the SOAP message --&gt;
  &lt;to uri=&quot;sql:UPDATE TRANSFER SET RESPONSE_XML = # WHERE ID = :prop1&quot;/&gt;
&lt;/route&gt;
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Portlets with XForms in Liferay by Lukas</title>
		<link>http://blog.mgm-tp.com/2011/01/rethinking-web-forms-xforms-part3/comment-page-1/#comment-4214</link>
		<dc:creator>Lukas</dc:creator>
		<pubDate>Wed, 17 Apr 2013 16:51:28 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mgm-tp.com/?p=824#comment-4214</guid>
		<description>Hi, wanted to ask whether you had any problems with filtered xform resources - javascript, css and images because we tried to show xform in liferay portlet using OrbeonPortletXFormsFilter and Trampoline servlet writing to the response whole form or with request.setAttribute("oxf.xforms.renderer.document", xformXml); Result is the same form is rendered but javascript and CSS are abscent all form is scattered and form actions doesn't work. Thank you.</description>
		<content:encoded><![CDATA[<p>Hi, wanted to ask whether you had any problems with filtered xform resources &#8211; javascript, css and images because we tried to show xform in liferay portlet using OrbeonPortletXFormsFilter and Trampoline servlet writing to the response whole form or with request.setAttribute(&#8220;oxf.xforms.renderer.document&#8221;, xformXml); Result is the same form is rendered but javascript and CSS are abscent all form is scattered and form actions doesn&#8217;t work. Thank you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Tuning Garbage Collection for Mission-Critical Java Applications by Dr. Andreas Müller</title>
		<link>http://blog.mgm-tp.com/2013/03/garbage-collection-tuning/comment-page-1/#comment-4197</link>
		<dc:creator>Dr. Andreas Müller</dc:creator>
		<pubDate>Tue, 16 Apr 2013 16:10:18 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mgm-tp.com/?p=1525#comment-4197</guid>
		<description>Hello Henry Ho, 
thank you for pointing me to BigMemory and DirectMemory.
These seem to be interesting options to deal with large amounts of long-lived objects which you want to get out of your old generation heap.  
As they come with a cost (for your application architecture and object management code), however, they do not look like an everyday solution to me. 
I would probably use such a solution when I can no longer efficiently handle such objects by GC tuning alone, not as a substitute for the basic GC tuning effort outlined in my post.</description>
		<content:encoded><![CDATA[<p>Hello Henry Ho,<br />
thank you for pointing me to BigMemory and DirectMemory.<br />
These seem to be interesting options to deal with large amounts of long-lived objects which you want to get out of your old generation heap.<br />
As they come with a cost (for your application architecture and object management code), however, they do not look like an everyday solution to me.<br />
I would probably use such a solution when I can no longer efficiently handle such objects by GC tuning alone, not as a substitute for the basic GC tuning effort outlined in my post.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Must-Know URL Hash Techniques for AJAX Applications by window.onhashchange, jQuery hashchange, history.pushState and History.js | JS-Area</title>
		<link>http://blog.mgm-tp.com/2011/10/must-know-url-hashtechniques-for-ajax-applications/comment-page-1/#comment-4172</link>
		<dc:creator>window.onhashchange, jQuery hashchange, history.pushState and History.js | JS-Area</dc:creator>
		<pubDate>Fri, 12 Apr 2013 06:55:30 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mgm-tp.com/?p=1006#comment-4172</guid>
		<description>[...] you got a glimpse of how these work, I suggest you read Must-Know URL Hash Techniques for AJAX Applications, which gives an overview of the whole [...]</description>
		<content:encoded><![CDATA[<p>[...] you got a glimpse of how these work, I suggest you read Must-Know URL Hash Techniques for AJAX Applications, which gives an overview of the whole [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Designing and Implementing our Camel-based mgm Cosmo Router by Claus Ibsen</title>
		<link>http://blog.mgm-tp.com/2013/04/camel-router-part1/comment-page-1/#comment-4161</link>
		<dc:creator>Claus Ibsen</dc:creator>
		<pubDate>Wed, 10 Apr 2013 08:22:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mgm-tp.com/?p=1513#comment-4161</guid>
		<description>I really enjoyed reading this blog.  Its well written and goes in depth, and also details why you did it this way. As well telling about the pitfalls and problems you had.

I took the liberty of adding a link to this blog entry from the Camel articles page (our link collection, takes a while to sync the change).
http://camel.apache.org/articles

From Camel 2.11 onwards the SQL component now also supports consumer side, so you don't need to trigger this with the timer component anymore.

Though a good idea with the "noop" so you can execute the SQL but dont change the message body. I will log a ticket to get this implement in the SQL component.

I agree about the parameter mappings. Its been improved in 2.11, to allow using named parameters. But so far it only grabs from body/headers. Not sure if there is a good way to define a syntax that don't get to verbose in the DSL. 

Some people use a template language to do the mapping and build dynamic queries. For example with freemarker / velocity etc.

PS: I personally like the spring jdbc template for SQL stuff. Its very flexible and you can do all in the Java code. 

Another alternative is MyBatis when you have more extensive mapping needs as its excellent for that. 

Looking forward to the next blogs.


/Claus Ibsen
Apache Camel committer</description>
		<content:encoded><![CDATA[<p>I really enjoyed reading this blog.  Its well written and goes in depth, and also details why you did it this way. As well telling about the pitfalls and problems you had.</p>
<p>I took the liberty of adding a link to this blog entry from the Camel articles page (our link collection, takes a while to sync the change).<br />
<a href="http://camel.apache.org/articles" rel="nofollow">http://camel.apache.org/articles</a></p>
<p>From Camel 2.11 onwards the SQL component now also supports consumer side, so you don&#8217;t need to trigger this with the timer component anymore.</p>
<p>Though a good idea with the &#8220;noop&#8221; so you can execute the SQL but dont change the message body. I will log a ticket to get this implement in the SQL component.</p>
<p>I agree about the parameter mappings. Its been improved in 2.11, to allow using named parameters. But so far it only grabs from body/headers. Not sure if there is a good way to define a syntax that don&#8217;t get to verbose in the DSL. </p>
<p>Some people use a template language to do the mapping and build dynamic queries. For example with freemarker / velocity etc.</p>
<p>PS: I personally like the spring jdbc template for SQL stuff. Its very flexible and you can do all in the Java code. </p>
<p>Another alternative is MyBatis when you have more extensive mapping needs as its excellent for that. </p>
<p>Looking forward to the next blogs.</p>
<p>/Claus Ibsen<br />
Apache Camel committer</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Boost your Website with great URLs by dating games</title>
		<link>http://blog.mgm-tp.com/2010/05/art-of-seo-part2/comment-page-1/#comment-4153</link>
		<dc:creator>dating games</dc:creator>
		<pubDate>Wed, 10 Apr 2013 03:54:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mgm-tp.com/?p=503#comment-4153</guid>
		<description>Hey theгe, I think your webѕite might be haνing brοwѕer cοmpatіbility 
issues. When I look at your blog site in Safari, it looks fine but when oρening in Internet Εxplorer, it has some ovеrlapping.
I just wantеd to give уou a quick heads up!
Other then that, very gοod blog!</description>
		<content:encoded><![CDATA[<p>Hey the&#1075;e, I think your web&#1109;ite might be ha&nu;ing br&#959;w&#1109;er c&omicron;mpat&#1110;bility<br />
issues. When I look at your blog site in Safari, it looks fine but when o&rho;ening in Internet &#917;xplorer, it has some ov&#1077;rlapping.<br />
I just want&#1077;d to give &#1091;ou a quick heads up!<br />
Other then that, very g&#959;od blog!</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Portlets with XForms in Liferay by Karri</title>
		<link>http://blog.mgm-tp.com/2011/01/rethinking-web-forms-xforms-part3/comment-page-1/#comment-4146</link>
		<dc:creator>Karri</dc:creator>
		<pubDate>Tue, 09 Apr 2013 06:31:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mgm-tp.com/?p=824#comment-4146</guid>
		<description>Hmm is anyone else encountering problems with the images on this 
blog loading? I'm trying to figure out if its a problem on my end or if it's the blog.
Any feedback would be greatly appreciated.</description>
		<content:encoded><![CDATA[<p>Hmm is anyone else encountering problems with the images on this<br />
blog loading? I&#8217;m trying to figure out if its a problem on my end or if it&#8217;s the blog.<br />
Any feedback would be greatly appreciated.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Tuning Garbage Collection for Mission-Critical Java Applications by Henry Ho</title>
		<link>http://blog.mgm-tp.com/2013/03/garbage-collection-tuning/comment-page-1/#comment-4089</link>
		<dc:creator>Henry Ho</dc:creator>
		<pubDate>Thu, 28 Mar 2013 18:51:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mgm-tp.com/?p=1525#comment-4089</guid>
		<description>Have you played with BigMemory or DirectMemory? http://incubator.apache.org/projects/directmemory.html  What are your thoughts on them?</description>
		<content:encoded><![CDATA[<p>Have you played with BigMemory or DirectMemory? <a href="http://incubator.apache.org/projects/directmemory.html" rel="nofollow">http://incubator.apache.org/projects/directmemory.html</a>  What are your thoughts on them?</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Tuning Garbage Collection for Mission-Critical Java Applications by ErikFWinter</title>
		<link>http://blog.mgm-tp.com/2013/03/garbage-collection-tuning/comment-page-1/#comment-4088</link>
		<dc:creator>ErikFWinter</dc:creator>
		<pubDate>Thu, 28 Mar 2013 15:59:02 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mgm-tp.com/?p=1525#comment-4088</guid>
		<description>Great article about Java GC. I'm looking forward to your next one about G1</description>
		<content:encoded><![CDATA[<p>Great article about Java GC. I&#8217;m looking forward to your next one about G1</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Developers, Don’t Write Functional Tests! by Mithat Perköz</title>
		<link>http://blog.mgm-tp.com/2010/11/successful-software-testing-part1/comment-page-1/#comment-4018</link>
		<dc:creator>Mithat Perköz</dc:creator>
		<pubDate>Wed, 20 Feb 2013 09:00:16 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mgm-tp.com/?p=777#comment-4018</guid>
		<description>Hi Martin,

Great article which will help me develop a full-fledged test driven software development life cycle. I'd love to see the part 2 of this article.

Thanks so much for your efforts.</description>
		<content:encoded><![CDATA[<p>Hi Martin,</p>
<p>Great article which will help me develop a full-fledged test driven software development life cycle. I&#8217;d love to see the part 2 of this article.</p>
<p>Thanks so much for your efforts.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Consequences when using Mutable Fields in hashCode() by Mentallurg</title>
		<link>http://blog.mgm-tp.com/2012/03/hashset-java-puzzler/comment-page-1/#comment-4017</link>
		<dc:creator>Mentallurg</dc:creator>
		<pubDate>Mon, 18 Feb 2013 20:23:27 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mgm-tp.com/?p=1123#comment-4017</guid>
		<description>You describe pretty common bug, that many junior developers do. But your analysis has some flaws.

1. "In general, any hash-based collection is problematic" - that is wrong.

Let's consider more simple case, array of Comparable objects. Suppose you have an array of Comparable objects, e.g. orderable by phone number:
  [{"444", "IBM"}, {"555", "MS"}, {"333", "MGM"}]

You call Arrays.sort(phoneNumbers) and the array becomes sorted ascending. Suppose it looks like follows:
  [{"333", "MGM"}, {"444", "IBM"}, {"555", "MS"}]
Then you modify the phone number of one of the objects: "555" -&gt; "111":
  [{"333", "MGM"}, {"444", "IBM"}, {"111", "MS"}]
The order is broken.

If you suppose that the last element has the biggest number, it is wrong now. If you suppose that the first element has the smallest number, it is wrong now.

Would you say "In general, any Comparable array is problematic"?

I guess no. The same about hash-based collections. They are not problematic. You just use them not properly and your expectations are wrong.


2. You describe HashSet.contains() and equals(), but you fail to see the essential contract of hashCode():
"If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result."

It is bad that you have not provided your implementation of equals(). Does it use both name and number? Or only name? Or only number? Or none (default from Object)?
Suppose you use both name and number.

- If your equals() uses number, than changing the number also changes the behaviour of equals(). Then the behaviour of the set   is perfectly CORRECT. Just see the documentation:
  "The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons ...".
  After you modified the number "in a manner that affects equals comparisons", you CANNOT expect any particular behaviour of any methods, including contains(). It means in particular that contains() can now return ANY value (true/false) for ANY object. Your expectation that contains(obj) will still return true is WRONG.
 
- If you equals() does not use 'number' (suppose you use 'name' only), then you are really allowed to expect that contains(obj) returns true. But in this case you have another error. Namely, your hashCode() violates the contract. If equals() doesn't use the number attribute and if two objects are equal, they MUST have the same value of the hashCode() -&gt; it means that hasCode() cannot depend on the number -&gt; it means your hashCode() is wrong, it is not allowed to use the number -&gt; if you fix the hashCode() according to the contract then the problem with HashSet disappears too.
 
- The same, but shorter:
  if your modification changes hashCode(), then it affects equals() too, (otherwise it would violate the hashCode() contract that equal objects have the same hash code).
  But if equals() changes, then according to the Set contract "The behavior of a set is not specified".
  It means you cannot expect any particular result from contains(obj).
 
 
Conclusion:
Your problem is not the HashSet, but your violation of several contracts:
- You ignore the contract between hashCode() and equals()
- You ignore the contract that "behavior of a set is not specified" if your modification affects equals()
- As a consequence of these your errors you erroneously suppose a design flaw in the hash-based collections
- You don't see that the same behaviour exists for other implementations like TreeSet

Basically, you should not modify objects during the "life cycle" of the set. Namely, there should not be cahnges that affect equals().

Solution:
I don't know what problem are you trying to solve. That's why I don't have ready solution for you.
- If you have a collection of companies and they are distinguished by the name, then equals() and hashCode() should only contain name, not a number. Then modifying the number will not cause any problems with Set.
- May be you need a unique ID (or a primary key in DB) to distinguish the objects
- May be you need a special kind of collection. Then forget hashCode(), keep in mind equals() only, and answer following question:
-- If one object in the collection is modified and becomes equal to another one, should the collection remove one of duplicates automatically? Which one?
-- If not remove automatically, then how should then work the remove() method? Should it remove all objects that equal to it, or only one of them? Which one?</description>
		<content:encoded><![CDATA[<p>You describe pretty common bug, that many junior developers do. But your analysis has some flaws.</p>
<p>1. &#8220;In general, any hash-based collection is problematic&#8221; &#8211; that is wrong.</p>
<p>Let&#8217;s consider more simple case, array of Comparable objects. Suppose you have an array of Comparable objects, e.g. orderable by phone number:<br />
  [{"444", "IBM"}, {"555", "MS"}, {"333", "MGM"}]</p>
<p>You call Arrays.sort(phoneNumbers) and the array becomes sorted ascending. Suppose it looks like follows:<br />
  [{"333", "MGM"}, {"444", "IBM"}, {"555", "MS"}]<br />
Then you modify the phone number of one of the objects: &#8220;555&#8243; -&gt; &#8220;111&#8243;:<br />
  [{"333", "MGM"}, {"444", "IBM"}, {"111", "MS"}]<br />
The order is broken.</p>
<p>If you suppose that the last element has the biggest number, it is wrong now. If you suppose that the first element has the smallest number, it is wrong now.</p>
<p>Would you say &#8220;In general, any Comparable array is problematic&#8221;?</p>
<p>I guess no. The same about hash-based collections. They are not problematic. You just use them not properly and your expectations are wrong.</p>
<p>2. You describe HashSet.contains() and equals(), but you fail to see the essential contract of hashCode():<br />
&#8220;If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.&#8221;</p>
<p>It is bad that you have not provided your implementation of equals(). Does it use both name and number? Or only name? Or only number? Or none (default from Object)?<br />
Suppose you use both name and number.</p>
<p>- If your equals() uses number, than changing the number also changes the behaviour of equals(). Then the behaviour of the set   is perfectly CORRECT. Just see the documentation:<br />
  &#8220;The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons &#8230;&#8221;.<br />
  After you modified the number &#8220;in a manner that affects equals comparisons&#8221;, you CANNOT expect any particular behaviour of any methods, including contains(). It means in particular that contains() can now return ANY value (true/false) for ANY object. Your expectation that contains(obj) will still return true is WRONG.</p>
<p>- If you equals() does not use &#8216;number&#8217; (suppose you use &#8216;name&#8217; only), then you are really allowed to expect that contains(obj) returns true. But in this case you have another error. Namely, your hashCode() violates the contract. If equals() doesn&#8217;t use the number attribute and if two objects are equal, they MUST have the same value of the hashCode() -&gt; it means that hasCode() cannot depend on the number -&gt; it means your hashCode() is wrong, it is not allowed to use the number -&gt; if you fix the hashCode() according to the contract then the problem with HashSet disappears too.</p>
<p>- The same, but shorter:<br />
  if your modification changes hashCode(), then it affects equals() too, (otherwise it would violate the hashCode() contract that equal objects have the same hash code).<br />
  But if equals() changes, then according to the Set contract &#8220;The behavior of a set is not specified&#8221;.<br />
  It means you cannot expect any particular result from contains(obj).</p>
<p>Conclusion:<br />
Your problem is not the HashSet, but your violation of several contracts:<br />
- You ignore the contract between hashCode() and equals()<br />
- You ignore the contract that &#8220;behavior of a set is not specified&#8221; if your modification affects equals()<br />
- As a consequence of these your errors you erroneously suppose a design flaw in the hash-based collections<br />
- You don&#8217;t see that the same behaviour exists for other implementations like TreeSet</p>
<p>Basically, you should not modify objects during the &#8220;life cycle&#8221; of the set. Namely, there should not be cahnges that affect equals().</p>
<p>Solution:<br />
I don&#8217;t know what problem are you trying to solve. That&#8217;s why I don&#8217;t have ready solution for you.<br />
- If you have a collection of companies and they are distinguished by the name, then equals() and hashCode() should only contain name, not a number. Then modifying the number will not cause any problems with Set.<br />
- May be you need a unique ID (or a primary key in DB) to distinguish the objects<br />
- May be you need a special kind of collection. Then forget hashCode(), keep in mind equals() only, and answer following question:<br />
&#8211; If one object in the collection is modified and becomes equal to another one, should the collection remove one of duplicates automatically? Which one?<br />
&#8211; If not remove automatically, then how should then work the remove() method? Should it remove all objects that equal to it, or only one of them? Which one?</p>
]]></content:encoded>
	</item>
</channel>
</rss><!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced
Database Caching using disk: basic

Served from: blog.mgm-tp.com @ 2013-05-18 12:22:35 -->
