Some days ago we switched to Java 7 within my Company - finally! Jay \o/ So I found out about the Objects
class and was astonished how short the methods hashCode()
and equals()
were realized, reducing a lot of boylerplate code compared to the ones generated by eclipse per default (ALT+SHIFT+S --> H).
I was wondering if I could change the default behaviour of the eclipse generated hashCode()
and equals()
?
I'd love to see this:
@Override
public int hashCode()
{
return Objects.hash(one, two, three, four/*, ...*/);
}
instead of this:
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((one == null) ? 0 : one.hashCode());
result = prime * result + ((two == null) ? 0 : two.hashCode());
result = prime * result + ((three == null) ? 0 : three.hashCode());
result = prime * result + ((four== null) ? 0 : four.hashCode());
// ...
return result;
}
The same goes for equals()
. This is the article I got this from.
Any ideas how to realize this best?