Is it possible to change the code generation template for equals()
and hashCode()
?
I would like the generated code to use the Java 7 Objects
class for theses methods.
You can use ⌘N (macOS), or Alt+Insert (Windows/Linux) for the Generate menu and then select equals() and hashCode() . You can also use the same shortcut again and select toString() to override that method as well.
The equals() and hashcode() are the two important methods provided by the Object class for comparing objects. Since the Object class is the parent class for all Java objects, hence all objects inherit the default implementation of these two methods.
The value returned by hashCode() is the object's hash code, which is the object's memory address in hexadecimal. equals() checks if the two object references are same. If two objects are equal then their hashCode must be the same, but the reverse is not true.
In HashMap, hashCode() is used to calculate the bucket and therefore calculate the index. equals() method: This method is used to check whether 2 objects are equal or not. This method is provided by the Object class. You can override this in your class to provide your implementation.
As of release 14.1 of the Ultimate Edition it is possible to customize the Code generation template of equals()/hashCode() without the use of any third party plugin.
Press "Alt + Insert" (Generate...), choose "equals() and hashCode()" and you will be able to select one of the predefined templates or a customized template of your own.
UPDATE: have a look at the answer from @datentyp. Leaving mine for those stuck on the old versions
There are plugins to allow this. Please have a look at this ones:
http://plugins.jetbrains.com/plugin/6875?pr=idea
http://plugins.jetbrains.com/plugin/7244?pr=idea
They are opensource so you can amend them if you need to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With