Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The commons/lang builders are great and I have been using them for years without noticeable performance overhead (with and without hibernate). But as Alain writes, the Guava way is even nicer:</p> <p>Here's a sample Bean:</p> <pre><code>public class Bean{ private String name; private int length; private List&lt;Bean&gt; children; } </code></pre> <p>Here's equals() and hashCode() implemented with Commons/Lang:</p> <pre><code>@Override public int hashCode(){ return new HashCodeBuilder() .append(name) .append(length) .append(children) .toHashCode(); } @Override public boolean equals(final Object obj){ if(obj instanceof Bean){ final Bean other = (Bean) obj; return new EqualsBuilder() .append(name, other.name) .append(length, other.length) .append(children, other.children) .isEquals(); } else{ return false; } } </code></pre> <p>and here with Java 7 or higher (inspired by Guava):</p> <pre><code>@Override public int hashCode(){ return Objects.hash(name, length, children); } @Override public boolean equals(final Object obj){ if(obj instanceof Bean){ final Bean other = (Bean) obj; return Objects.equals(name, other.name) &amp;&amp; length == other.length // special handling for primitives &amp;&amp; Objects.equals(children, other.children); } else{ return false; } } </code></pre> <p>Note: this code originally referenced Guava, but as comments have pointed out, this functionality has since been introduced in the JDK, so Guava is no longer required.</p> <p>As you can see the Guava / JDK version is shorter and avoids superfluous helper objects. In case of equals, it even allows for short-circuiting the evaluation if an earlier <code>Object.equals()</code> call returns false (to be fair: commons / lang has an <code>ObjectUtils.equals(obj1, obj2)</code> method with identical semantics which could be used instead of <code>EqualsBuilder</code> to allow short-circuiting as above).</p> <p>So: yes, the commons lang builders are very preferable over manually constructed <code>equals()</code> and <code>hashCode()</code> methods (or those awful monsters Eclipse will generate for you), but the Java 7+ / Guava versions are even better.</p> <p>And a note about Hibernate:</p> <p>be careful about using lazy collections in your equals(), hashCode() and toString() implementations. That will fail miserably if you don't have an open Session.</p> <hr> <p>Note (about equals()):</p> <p>a) in both versions of equals() above, you might want to use one or both of these shortcuts also:</p> <pre><code>@Override public boolean equals(final Object obj){ if(obj == this) return true; // test for reference equality if(obj == null) return false; // test for null // continue as above </code></pre> <p>b) depending on your interpretation of the equals() contract, you might also change the line(s)</p> <pre><code> if(obj instanceof Bean){ </code></pre> <p>to</p> <pre><code> // make sure you run a null check before this if(obj.getClass() == getClass()){ </code></pre> <p>If you use the second version, you probably also want to call <code>super(equals())</code> inside your <code>equals()</code> method. Opinions differ here, the topic is discussed in this question: </p> <blockquote> <p><a href="https://stackoverflow.com/q/8248970/342852">right way to incorporate superclass into a Guava Objects.hashcode() implementation?</a></p> </blockquote> <p>(although it's about <code>hashCode()</code>, the same applies to <code>equals()</code>)</p> <hr> <p><strong>Note (inspired by Comment from <a href="https://stackoverflow.com/users/274473/kayahr">kayahr</a>)</strong></p> <p><code>Objects.hashCode(..)</code> (just as the underlying <code>Arrays.hashCode(...)</code>) might perform badly if you have many primitive fields. In such cases, <code>EqualsBuilder</code> may actually be the better solution.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload