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

前端 未结 2 1087
死守一世寂寞
死守一世寂寞 2020-12-30 03:19

It is recommended and sometimes necessary, classes that represent values (value classes) to override hashCode(), equals() [and optionally

2条回答
  •  既然无缘
    2020-12-30 03:38

    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.

提交回复
热议问题