开发者

Overriding equals method in DTO's

开发者 https://www.devze.com 2023-03-31 18:00 出处:网络
Is it necessary to override equals and hashcode methods in DTO\'s? Because DTO\'s are ju开发者_开发百科st use for transfer data. Is there any best practice or something regarding this?

Is it necessary to override equals and hashcode methods in DTO's? Because DTO's are ju开发者_开发百科st use for transfer data. Is there any best practice or something regarding this?

Thanks.


This article offers one piece of advice:

Objects placed in a List , Set, or Map (as either a key or value) should have an appropriate definition of equals.

Surely DTOs are used for more than just transfer, we do keep them, sort them, cache them ...

In practice do folks provide equals and hash? No not always. Should we? I think so.


Whether or not you need to provide equals and hashcode implementations for your DTO classes depends on how you use them.

If you use them with one or more Collections, you should provide the implementation for the appropriate method. Almost all Collections call equals on the objects they store. Hash table based Collections like HashSet and HashMap call hashcode, whereas sorted Collections like TreeSet and TreeMap call compareTo method in addition to equals.


If it becomes too trivial I can recommend using the lombok annotations http://projectlombok.org/features/

0

精彩评论

暂无评论...
验证码 换一张
取 消