Overriding the java equals() method - not working?

后端 未结 8 1392
花落未央
花落未央 2020-11-22 02:33

I ran into an interesting (and very frustrating) issue with the equals() method today which caused what I thought to be a well tested class to crash and cause a

8条回答
  •  梦谈多话
    2020-11-22 03:13

    Slightly off-topic to your question, but it's probably worth mentioning anyway:

    Commons Lang has got some excellent methods you can use in overriding equals and hashcode. Check out EqualsBuilder.reflectionEquals(...) and HashCodeBuilder.reflectionHashCode(...). Saved me plenty of headache in the past - although of course if you just want to do "equals" on ID it may not fit your circumstances.

    I also agree that you should use the @Override annotation whenever you're overriding equals (or any other method).

提交回复
热议问题