Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>Map</code> implementations use the <code>hashCode()</code> method in an object to determine the key to look up in their internal data structures when you call <code>get</code>. In your specific example, let's assume that your <code>MyObject</code> class had an <code>id</code> property:</p> <pre><code>public class MyObject { private int id; public MyObject(int id) { this.id = id; } public int getId() { return id; } } </code></pre> <p>Assume you wanted the <code>id</code> property to be used when looking up keys in a map - irrespective of the instance of the object - you'd override the <code>hashCode</code> method in your class to do this:</p> <pre><code>public class MyObject { private int id; public MyObject(int id) { this.id = id; } public int getId() { return id; } /** * Uses the Jakarta commons-lang HashCodeBuilder class to generate the hash code. * * @see http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder * /HashCodeBuilder.html */ public int hashCode() { return new HashCodeBuilder(1, 5) .append(id) .toHashCode(); } public boolean equals(Object other) { if (other == null) return false; if (other == this) return true; return (other.id == this.id); } } </code></pre> <p>The <a href="http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/HashCodeBuilder.html#HashCodeBuilder%28int,%20int%29" rel="nofollow">documentation</a> for HashCodeBuilder explains what the numbers 1 and 5, passed as arguments to the HashCodeBuilder, do - I picked them randomly: the documentation says that they must be unique, random, odd numbers.</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. This table or related slice is empty.
    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