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
|
@Override public 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; } @Override public 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
|
@Override public boolean equals(Object obj) { return EqualsBuilder.reflectionEquals( this , obj); } @Override public 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 ...
随机推荐
- 一款功能强大的iphone购物应用源码
一款功能强大的iphone购物应用源码,这款应用源码比较完整的,并且还支持信用卡支付服务等功能的,基本实现了我们常用的购物应用功能了,实现商品的基本展示功能,还具有完整的用户管理,以及完整的购物流程等 ...
- grep恢复误删除文件内容(转)
在 Linux 上如果事先没有用别名(alias)修改默认的 rm 功能,rm 后文件就会丢失,幸运的是,在一般的删除文件操作中,Linux 并不会立即清空存储该文件的 block 内容,而只会释放该 ...
- 用 Function.apply() 的参数数组化来提高 JavaScript程序性能
我们再来聊聊Function.apply() 在提升程序性能方面的技巧. 我们先从 Math.max() 函数说起, Math.max后面可以接任意个参数,最后返回所有参数中的最大值. 比如 aler ...
- apache问题集锦
一.如何防止别的网站盗连我们网站的图片.CSS.JS等资源? RewriteCond %{HTTP_REFERER} !test.test.com [NC] #RewriteRule \.(gif|j ...
- apache 配置虚拟主机
1 打开httpd.conf 找到 # Virtual hosts #Include conf/extra/httpd-vhosts.conf [由于apache的版本不同,可能你ctrl+F ...
- TDirectory.CreateDirectory 完整、严谨的创建一个目录
描述:创建一个目录,不包含多级目录(多级目录使用System.SysUtils.ForceDirectories,Vcl.FileCtrl.ForceDirectories已过时) procedure ...
- glibc学习介绍篇
C语言自身并没有提供IO,内存管理,字符串操作等类似的机制.作为弥补,C语言有一个标准库帮助C语言实现这些机制.我们在编译C程序的时候基本上都需要链接到这些库文件. GNU C Library定义IS ...
- JAVA的字节码技术
1.什么是字节码? 字节码 byteCode JVM能够解释执行的.java程序的归宿,但是从规范上来讲和Java已没有任何关系了.一些动态语言也可以编译成字节码在JVM上运行.字节码就相当于JVM上 ...
- Android系统SVC命令教程
svc命令,位置在/system/bin目录下,用来管理电源控制,无线数据,WIFI # svc svc Available commands: help Show information about ...
- Android之EditText
EditText 属性介绍: maxLength:设置最大输入字符数. hint:设置空白提示文字. textColorHint:设置空白提示文字的颜色. enabled:设置是否可编辑(可以获得焦点 ...