- On the main menu, choose . Alternatively, right-click the editor and choose Generate on the context menu, or use Alt+Insert keyboard shortcut.
- 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.
- On the first page of the wizard, select the fields that should be used to determine equality, and click Next.
- 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.
- 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.
If equals() and hashCode() methods already exist in class, you will be prompted to delete them before proceeding.
Example
public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; FixedRateCurrencyExchangeService that = (FixedRateCurrencyExchangeService) o; if (Double.compare(that.rate, rate) != 0) return false; return true; } public int hashCode() { long temp = rate != +0.0d ? Double.doubleToLongBits(rate) : 0L; return int (temp ^ (temp >>> 32)); }
