Java:自动equals()和hashCode() [英] Java: Automatic equals() and hashCode()

查看:187
本文介绍了Java:自动equals()和hashCode()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为简单数据实现 equals() hashCode() POJO使我的代码混乱,维护繁琐。

Implementing equals() and hashCode() for simple data POJOs is cluttering my code and maintaining is tedious.

有哪些库会自动处理?

由于性能原因,我更喜欢字节码检测而不是AOP方法。

What are the libraries handling this automatically?
I prefer bytecode instrumentation over AOP approach due to performance reasons.

更新:已经讨论了实现equals()和hashCode()的必要性的主题,这是我的观点:

Update: Topic of necessity of implementing equals() and hashCode() has been discussed, here's my point:

以最小的努力提前完成它而不是挖掘代码,添加hC / eq是不是更好?

Isn't it better to have it done right upfront with minimal effort rather than digging in the code, adding hC/eq when it comes to it?

推荐答案

Project Lombok 提供注释 @ EqualsAndHashCode 将生成 equals() hashCode()用于您的Java类。当然,与手动实现这些方法相比,存在一些缺点,因此请务必阅读链接页面上的小字体。

Project Lombok provides the annotation @EqualsAndHashCode which will generate equals() and hashCode() for your Java classes. Of course there are some drawbacks in comparison to manually implementing these methods, so make sure you read the "small print" on the linked page.

这篇关于Java:自动equals()和hashCode()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆