<p>I have always thought that the .equals() method in java should be overridden to be made specific to the class you have created. In other words to look for equivalence of two different instances rather than two references to the same instance. However I have encountered other programmers who seem to think that the default object behavior should be left alone and a new method created for testing equivalence of two objects of the same class. </p>

<p>What are the argument for and against overriding the equals method?</p>

[{'answer_id': 16562, 'author': 'juan', 'author_id': 1782, 'author_profile': 'https://Stackoverflow.com/users/1782', 'pm_score': 0, 'selected': False, 'text': '<p>The Equals method is intended to compare references. So it should not be overriden to change its behaviour.</p>\n\n<p>You should create a new method to test for equivalence in different instances if you need to (or use the CompareTo method in some .NET classes)</p>\n'}, {'answer_id': 16573, 'author': 'McDowell', 'author_id': 304, 'author_profile': 'https://Stackoverflow.com/users/304', 'pm_score': 5, 'selected': True, 'text': '<p>Overriding the equals method is necessary if you want to test equivalence in standard library classes (for example, ensuring a java.util.Set contains unique elements or using objects as keys in java.util.Map objects).</p>\n\n<p>Note, if you override equals, ensure you honour the API contract as described in the documentation. For example, ensure you also override <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html" rel="noreferrer">Object.hashCode</a>:</p>\n\n<blockquote>\n  <p>If two objects are equal according to\n  the equals(Object)  method, then\n  calling the hashCode method on each of\n  the two objects must produce the same\n  integer result.</p>\n</blockquote>\n\n<p>EDIT: I didn\'t post this as a complete answer on the subject, so I\'ll echo Fredrik Kalseth\'s statement that overriding equals works best for <a href="http://en.wikipedia.org/wiki/Immutable_object" rel="noreferrer">immutable objects</a>. To quote the API for <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/Map.html" rel="noreferrer">Map</a>:</p>\n\n<blockquote>\n  <p>Note: great care must be exercised if\n  mutable objects are used as map keys.\n  The behavior of a map is not specified\n  if the value of an object is changed\n  in a manner that affects equals\n  comparisons while the object is a key\n  in the map.</p>\n</blockquote>\n'}, {'answer_id': 16574, 'author': 'Michael Sharek', 'author_id': 1958, 'author_profile': 'https://Stackoverflow.com/users/1958', 'pm_score': 0, 'selected': False, 'text': '<p>You should only need to override the <code>equals()</code> method if you want specific behaviour when adding objects to sorted data structures (<code>SortedSet</code> etc.)</p>\n\n<p>When you do that you should also override <code>hashCode()</code>.</p>\n\n<p>See <a href="http://www.geocities.com/technofundo/tech/java/equalhash.html" rel="nofollow noreferrer">here</a> for a complete explanation.</p>\n'}, {'answer_id': 16591, 'author': 'David Schlosnagle', 'author_id': 1750, 'author_profile': 'https://Stackoverflow.com/users/1750', 'pm_score': 3, 'selected': False, 'text': '<p>I would highly recommend picking up a copy of Effective Java and reading through item 7 obeying the <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html" rel="noreferrer">equals contract</a>. You need to be careful if you are overriding equals for mutable objects, as many of the collections such as Maps and Sets use equals to determine equivalence, and mutating an object contained in a collection could lead to unexpected results. Brian Goetz also has a pretty good <a href="http://www.ibm.com/developerworks/java/library/j-jtp05273.html" rel="noreferrer">overview of implementing equals and hashCode</a>.</p>\n'}, {'answer_id': 16601, 'author': 'Fredrik Kalseth', 'author_id': 1710, 'author_profile': 'https://Stackoverflow.com/users/1710', 'pm_score': 2, 'selected': False, 'text': '<p>You should "never" override equals &amp; getHashCode for mutable objects - this goes for .net and Java both. If you do, and use such an object as the key in f.ex a dictionary and then <em>change</em> that object, you\'ll be in trouble because the dictionary relies on the hashcode to find the object.</p>\n\n<p>Here\'s a good article on the topic: <a href="http://weblogs.asp.net/bleroy/archive/2004/12/15/316601.aspx" rel="nofollow noreferrer">http://weblogs.asp.net/bleroy/archive/2004/12/15/316601.aspx</a></p>\n'}, {'answer_id': 16816, 'author': 'serg10', 'author_id': 1853, 'author_profile': 'https://Stackoverflow.com/users/1853', 'pm_score': 0, 'selected': False, 'text': '<p>To be honest, in Java there is not really an argument against overriding <em>equals</em>.  If you need to compare instances for equality, then that is what you do.  </p>\n\n<p>As mentioned above, you need to be aware of the contract with <em>hashCode</em>, and similarly, watch out for the gotchas around the <a href="http://java.sun.com/javase/6/docs/api/java/lang/Comparable.html" rel="nofollow noreferrer">Comparable</a> interface - in almost all situations you want the <em>natural ordering</em> as defined by Comparable to be <em>consistent with equals</em> (see the <a href="http://java.sun.com/javase/6/docs/api/java/math/BigDecimal.html" rel="nofollow noreferrer">BigDecimal</a> api doc for the canonical counter example)</p>\n\n<p>Creating a new method for deciding equality, quite apart from not working with the existing library classes, flies in the face of Java convention somewhat.</p>\n'}, {'answer_id': 16922, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 2, 'selected': False, 'text': '<p>@David Schlosnagle <a href="https://stackoverflow.com/questions/16557/overiding-the-equals-method-vs-creating-a-new-method#16591">mentions</a> mentions Josh Bloch\'s <a href="https://rads.stackoverflow.com/amzn/click/com/0321356683" rel="nofollow noreferrer" rel="nofollow noreferrer">Effective Java</a> -- this is a <strong>must-read</strong> for any Java developer.</p>\n\n<p>There is a related issue: for immutable value objects, you should also consider overriding <code>compare_to</code>.  The standard wording for if they differ is in the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/Comparator.html" rel="nofollow noreferrer">Comparable API</a>:</p>\n\n<blockquote>\n  <p>It is generally the case, but not strictly required that (compare(x, y)==0) == (x.equals(y)). Generally speaking, any comparator that violates this condition should clearly indicate this fact. The recommended language is "Note: this comparator imposes orderings that are inconsistent with equals."</p>\n</blockquote>\n'}]
['https://Stackoverflow.com/questions/16557', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1104/']