Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize hashCode() and equals() generated by Eclipse?

It is recommended and sometimes necessary, classes that represent values (value classes) to override hashCode(), equals() [and optionally toString()] methods. The values that these methods return depend on all or subset of the member variables of the class and its super-class. To implement them properly you have to know a little bit of theory about hashing and a little bit of algebra and set theory (not too much, and almost everything is explaind in the javadocs for these methods and in Effective Java form Josh Bloch.)
In most of the cases, the implementation of this methods follow a template, and IDEs (like Eclipse JDT) include tools to generate them. However, the tool generators, can not make any assumptions, and generate these methods using only constructs available in the language and the standard library. Because of that these methods usually look very ugly.

Another way to implement these methods is to use library like Apache's (commons-lang) HashCodeBuilder, EqualsBuilder and ToStringBuilder. Using these utilities, one can implement their own hashCode() and equals() methods that look much better.

My question is about combining these two approaches. I would like to be able to customize Eclipse's hashCode() and equals() generators, so that will generate them using HashCodeBuilder and friends. Is it possible (and how) to do this without tweaking the JDT? Only writing small plugin that will override the default implementations (but without changing JDT code).

Thanks.

like image 803
Op De Cirkel Avatar asked Jun 06 '11 16:06

Op De Cirkel


People also ask

How do we implement hashCode () and equals ()?

Java hashCode() An object hash code value can change in multiple executions of the same application. If two objects are equal according to equals() method, then their hash code must be same. If two objects are unequal according to equals() method, their hash code are not required to be different.

How hashCode () and equals () methods are used 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.

How do you correctly override the hashCode () and equals () methods in Java?

if a class overrides equals, it must override hashCode. when they are both overridden, equals and hashCode must use the same set of fields. if two objects are equal, then their hashCode values must be equal as well. if the object is immutable, then hashCode is a candidate for caching and lazy initialization.


2 Answers

Posting my comment as an answer by request: Commonclipse, an Eclipse plugin that facilitates the use of Apache Commons, does what you want to do.

Caveat: I have no recent experience with this plugin, which is why I originally posted as a comment, and not as an answer.

like image 155
Nathan Ryan Avatar answered Oct 20 '22 18:10

Nathan Ryan


In the eclipse preferences (Window>Preferences) go to Java > Editor > Templates.

In there you can create a teplate with name:hashcode context:java description:Create a hashcode method. The Pattern should contain something like this:

public int hashCode() {
    return HashCodeBuilder.reflectionHashCode(this);
}

Save and return to your java class. Type the name (hashcode) and press ctrl enter. You can then select your template from the drop down list.

Do the same for each method you want. You could also create a template that combines everything together as well.

like image 30
Heathen Avatar answered Oct 20 '22 18:10

Heathen