Maintainable HashCode and Equals Using Apache Commons
Java hashCode and equals methods can be tricky to implement correctly. Fortunately, all majors IDEs allow generating them. For example, this is how they look like for a class with two attributes when generated in Eclipse:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
@Overridepublic int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((firstName == null) ? 0 : firstName.hashCode()); result = prime * result + ((lastName == null) ? 0 : lastName.hashCode()); return result;}@Overridepublic boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (firstName == null) { if (other.firstName != null) return false; } else if (!firstName.equals(other.firstName)) return false; if (lastName == null) { if (other.lastName != null) return false; } else if (!lastName.equals(other.lastName)) return false; return true;} |
It’s better than writing all this by hand but I still don’t like it. Here’s why:
- It makes it way too easy to forget about updating them after adding a new field
- It lowers the code coverage when not properly tested
- It is just plain ugly
The first reason is the most important one. Having to remember about updating generated equals and hashCode methods when adding new fields increases the maintenance cost. Forgetting to do so may result in nasty bugs.
Because of that I never use generated hashCode and equals. I use builders from Apache Commons instead. In the most basic scenario they look like this:
|
1
2
3
4
5
6
7
8
9
|
@Overridepublic boolean equals(Object obj) { return EqualsBuilder.reflectionEquals(this, obj);}@Overridepublic int hashCode() { return HashCodeBuilder.reflectionHashCode(this);} |
They retrieve values using reflection from all fields except for transient and static ones. Most of the time this is precisely what I want. If you need some customizations check out the API documentation.
Maintainable HashCode and Equals Using Apache Commons的更多相关文章
- hashCode()和equals()的用法
使用hashCode()和equals() hashCode()和equals()定义在Object类中,这个类是所有java类的基类,所以所有的java类都继承这两个方法. hashCode()方法 ...
- Java 中正确使用 hashCode 和 equals 方法
在这篇文章中,我将告诉大家我对hashCode和equals方法的理解.我将讨论他们的默认实现,以及如何正确的重写他们.我也将使用Apache Commons提供的工具包做一个实现. 目录: hash ...
- Java中正确使用hashCode() 和equals() 、==
在java编程中,经常会遇到两个对象中某个属性的比较,常常会用到的方法有: == .equals().但是两者使用起来有什么区别呢? 一.== java中的==是比较两个对象在JVM中的地址.比较好理 ...
- (转)Java 中正确使用 hashCode 和 equals 方法
背景:最近在编写持久化对象时候遇到重写equals和hashCode方法的情况,对这两个方法的重写做一个总结. 链接:https://www.oschina.net/question/82993_75 ...
- Java中正确使用hashCode和equals方法
在这篇文章中,我将告诉大家我对hashCode和equals方法的理解.我将讨论他们的默认实现,以及如何正确的重写他们.我也将使用Apache Commons提供的工具包做一个实现. 目录: hash ...
- Apache Commons Lang
http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/package- ...
- 【java】org.apache.commons.lang3功能示例
org.apache.commons.lang3功能示例 package com.simple.test; import java.util.Date; import java.util.Iterat ...
- 一篇关于apache commons类库的详解
1.1. 开篇 在Java的世界,有很多(成千上万)开源的框架,有成功的,也有不那么成功的,有声名显赫的,也有默默无闻的.在我看来,成功而默默无闻的那些框架值得我们格外的尊敬和关注,Jakarta C ...
- Java工具类 Apache Commons:commons-lang
Commons Lang The standard Java libraries fail to provide enough methods for manipulation of its core ...
随机推荐
- 安卓4.0下rem显示不正常的问题
在项目中使用了rem为单位,结果在Oppo和4.0下某些浏览器rem工作不正常,font-size计算出来的px总是大于预期的值,因此加了个Hack var docEl = doc.documentE ...
- centos中安装chromium和flash
安装环境:centos 6.5 64位 在centos中安装chromium 安装Google源 cd /etc/yum.repos.d/ sudo wget http://people.CentOS ...
- [大牛翻译系列]Hadoop(14)MapReduce 性能调优:减小数据倾斜的性能损失
6.4.4 减小数据倾斜的性能损失 数据倾斜是数据中的常见情况.数据中不可避免地会出现离群值(outlier),并导致数据倾斜.这些离群值会显著地拖慢MapReduce的执行.常见的数据倾斜有以下几类 ...
- maven 相关
maven相关 一 windows安装配置maven: 1. 官网下载最新版本maven,发布日志时为: maven3.2.1 2.解压maven到相应的目录:配置环境变量: MAVEN_HOME:D ...
- 一款jQuery仿海尔官网全屏焦点图特效代码
一款jQuery仿海尔官网全屏焦点图特效代码,带有左右箭头的jQuery焦点图切换特效. 当焦点图切换时,下方的三块小图也相对应的进行切换.并且特效还兼容头疼的IE6.赶快去和谐了它吧! 适用浏览器: ...
- Android Material Design的FloatingActionButton,Snackbar和CoordinatorLayout
如果是为了兼容低版本的Android系统,则需要引用Android Material Design的扩展支持库,我在之前的一篇文章张,较为详细的说明了如何导入Android Material Desi ...
- 开源免费的C/C++网络库(c/c++ sockets library)
(1)ACE 庞大.复杂,适合大型项目.开源.免费,不依赖第三方库,支持跨平台. http://www.cs.wustl.edu/~schmidt/ACE.html (2)Asio Asio基于Boo ...
- 拥抱ARM妹纸第二季 之 第二次 约会需要浪漫,这么大灯泡怎么弄?
终于轮到俺的小穆出场啦.有请能让太阳也为之暗淡的小穆闪亮登场-,鼓掌吧,欢呼吧!-- ♪♪ We can burn brighter Than the sun ~~~ ♪♪ “谢谢---“ 唱的太棒啦 ...
- tomcat中server.xml文件解析
下面我们将讲述这个文件中的基本配置信息,更具体的配置信息见tomcat的文档 元素名 属性 解释 server port 指定一个端口,这个端口负责监听关闭tomcat的请求 shutdown 指定向 ...
- gtest功能测试一
一.前言 这篇文章主要总结gtest中的所有断言相关的宏. gtest中,断言的宏可以理解为分为两类,一类是ASSERT系列,一类是EXPECT系列.一个直观的解释就是: 1. ASSERT_* 系列 ...