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 ...
随机推荐
- [Silverlight] Visual Studio2010不能安装Silverlight4_Tools,提示语言不一致
今天在装Silverlight4_Tools时出现“必须先安装与 Silverlight Tools 4 语言版本相一致的 Visual Studio 2010.Visual Web Develope ...
- C++ Vector 动态数组
Vectors 包含着一系列连续存储的元素,其行为和数组类似.访问Vector中的任意元素或从末尾添加元素都可以在常量级时间复杂度内完成,而查找特定值的元素所处的位置或是在Vector中插入元素则是线 ...
- 【风马一族_Java】如何使用ACSLL表的值,
------------------------------------------------------------------------------ 一,依次ACSLL表的值 将自然数赋值给c ...
- JavaScript 内部对象
欢迎访问我的个人博客:http://blog.cdswyda.com/ 原文文地址:JavaScript 内部对象
- 基础学习总结(八)--Intent中显示意图和隐式意图的用法
Intent(意图)主要是解决Android应用的各项组件之间的通讯.Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组 ...
- Cookie与Session详解
来源:<PHP核心技术与最佳实践> 列旭松 陈文 著 Cookie与Session详解读书笔记,从概念.操作.应用.注意事项以及区别等几方面详细阐述两者的基础知识,它们都是针对HTTP协议 ...
- WPF 绑定四(层级绑定)
xaml: <Window x:Class="WpfApplication1.Window4" xmlns="http://schemas.microsoft.co ...
- mysql rand随机查询记录效率
一直以为mysql随机查询几条数据,就用 SELECT * FROM `table` ORDER BY RAND() LIMIT 5 就可以了. 但是真正测试一下才发现这样效率非常低.一个15万余条的 ...
- 常用ASCII 码对照表
目前计算机中用得最广泛的字符集及其编码,是由美国国家标准局(ANSI)制定的ASCII码(American Standard Code for Information Interchange,美国标准 ...
- spring中controller
提示:原网站已由百度转码,以便在移动设备上查看. 第七城市 (Portal 开发读书笔记)Spring Portlet MVC 测试Controller 2012-04-28 16:32:44 - - ...