Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The warnings only says that <code>x.hashCode() != Objects.hashCode(x)</code> is true. (Okay, this is true most of the time. They could still collide for some values. It's actually not equal for <strong>most</strong> objects.)</p> <p>A valid hashCode/equals implementation is:</p> <pre><code>public class Teacher { private final String name; public Teacher(String name) { this.name = name; } @Override public equals(Object obj){ if(obj == this) return true; if(!(obj instanceof Teacher)) return false; return Objects.equal(name, ((Teacher) obj).getName()); } @Override public hashCode(){ return 0; } } </code></pre> <p>This is a valid although all hash values collide. From hashCode() javadoc:</p> <blockquote> <p>It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results.</p> </blockquote> <p>The difference to a "normal" implementation is that the performance of this code will be much worse. HashMaps for example will degenerate to a lists like performance for lookups.</p> <p>Even with this implementation:</p> <pre><code>@Override public int hashCode() { return Objects.hashCode(Teacher.class, name); } </code></pre> <p>it's possible (but very unlikely) that the hash values of different classes collide. This is the case if the hashes of the class names are the same for both classes. </p> <p>This kind of optimization should be a last resort when there are <strong>a lot</strong> of instances from different types with the <strong>same name</strong> in a collection that <strong>uses hashCode() internally</strong>. The overall efect will be limited: if you have n types you have at most n collisions due to this scenario. Other factors are probable to dominate the performance characteristics. </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. 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