Generate hashCode() and equals()

Why does IDEA generate this code:

"if(!Arrays.equals(data, binary.data)) return false;

return true;"

instead of

"return Arrays.equals(data, binary.data);"

2 comments

I can't say for sure but it's very probably because the generation creates a line like
if <field-x equals other-field-x> return false;
for each of the fields of the class plus a single line
return true;
at the end.

If you happen to have only a single field in the class you end up with this degenerated form.

You could search Jira for a similar issue (there are quite a couple of issues regarding code generation of equals() and hashCode()) and then vote or create a new issue.

0

Ok, sure, let me rephrase:
Why doesn't IDEA do this for single (or few) fields?

regards,

Messi

Stephen Friedrich wrote:

I can't say for sure but it's very probably because the generation creates a line like
if <field-x equals other-field-x> return false;
for each of the fields of the class plus a single line
return true;
at the end.

If you happen to have only a single field in the class you end up with this degenerated form.

You could search Jira for a similar issue (there are quite a couple of issues regarding code generation of equals() and hashCode()) and then vote or create a new issue.

0

Please sign in to leave a comment.