Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to change IntelliJ's code generation template for equals() and hashCode()

Tags:

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.

like image 837
Mike Rylander Avatar asked Aug 21 '13 16:08

Mike Rylander


People also ask

How override equals and hashCode method in IntelliJ?

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.

What is the hashCode () and equals () used for?

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.

What is the difference between hash code and equals?

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.

What is the use of hashCode and equals method in HashMap?

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.


2 Answers

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.

"Generate equals() and hashCode()" dialog

Edit "equals() and hashCode()" Velocity templates

like image 58
datentyp Avatar answered Sep 24 '22 13:09

datentyp


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.

like image 21
klor Avatar answered Sep 23 '22 13:09

klor