IntelliJ IDEA 14.0.0 Web Help

Generating equals() and hashCode()

public boolean equals(Object obj):
This method returns true, if an object passed to it as an argument is equal to the object on which this method is invoked.

public int hashCode():
This method returns the integer hash code value for the object on which this method is invoked.

To generate equals() and hashcode() methods

  1. On the main menu, choose Code | Generate . Alternatively, right-click the editor and choose Generate on the context menu, or use Alt+Insert keyboard shortcut.
  2. In the pop-up list that is displayed in the editor, select equals() and hashCode() option. Generate equals() and hashCode() wizard displays the list of fields in the class.
  3. On the first page of the wizard, select the fields that should be used to determine equality, and click Next.
  4. On the second page, select the fields to generate hash code.

    Note that only the fields that were included in the equals() method, can participate in creating hash code. All these fields are selected by default, but you can unselect them, if necessary.

    Click Next.

  5. On the third page of the wizard, select the fields that contain non-null values. This optional step help the generated code avoid check for null and thus improves performance. Click Finish.


public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; FixedRateCurrencyExchangeService that = (FixedRateCurrencyExchangeService) o; if (, rate) != 0) return false; return true; } public int hashCode() { long temp = rate != +0.0d ? Double.doubleToLongBits(rate) : 0L; return int (temp ^ (temp >>> 32)); }

See Also

Last modified: 4 December 2014
comments powered by Disqus