云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > boss代码review记录

boss代码review记录

  • 62 次阅读
  • 3 次下载
  • 2025/5/31 11:10:06

1.21.1 错误示例

1.21.2 Bug描述

Bug: its.sso.sync.rmc.pojo.User defines equals and uses Object.hashCode() Patternid: HE_EQUALS_USE_HASHCODE, type: HE, category: BAD_PRACTICE This class overrides equals(Object), but does not override hashCode(), and inherits the implementation of hashCode() from java.lang.Object (which returns the identity hash code, an arbitrary value assigned to the object by the VM). Therefore, the class is very likely to violate the invariant that equal objects must have equal hashcodes. If you don't think instances of this class will ever be inserted into a HashMap/HashTable, the recommended hashCode implementation to use is: public int hashCode() { assert false : \ return 42; // any arbitrary constant will do } 1.21.3 解决方案

重载了equals方法,却没有重载hashCode方法,如果使用object自己的hashCode,我们可以从JDK源代码可以看到object的hashCode方法是native的,它的值由虚拟机分配(某种情况下代表了在虚拟机中的地址或者唯一标识),每个对象都不一样。所以这很可能违

反“Equals相等,hashcode一定相等;hashcode相等,equals不一定相等。”除非你保证不运用到HashMap/HashTable等运用散列表查找值的数据结构中。否则,发生任何事情都是有可能的。 关于何时改写hashcode,请参考:在重写了对象的equals方法后,还需要重写hashCode方法吗? 关于编写高质量的equals方法: 1.先使用==操作符检查是否是同一个对象,==都相等,那么逻辑相等肯定成立; 2.然后使用instanceof操作符检查“参数是否为正确的类型”; 3.把参数转换成正确的类型; 4.对于该类中的非基本类型变量,递归调用equals方法; 5.变量的比较顺序可能会影响到equals方法的性能,应该最先比较最有可能不一致的变量,或者是开销最低的变量。 当你编写完成equals方法之后,应该问自己三个问题:它是否是对称的、传递的、一致的? 解决方法: 除非你保证不运用到HashMap/HashTable等运用散列表查找值的数据结构中,请重写hashcode方法。 1.21.4 自动生成的

@Override publicint hashCode() { finalint prime = 31; int result = 1; result = prime * result + ((add1 == null) ? 0 : add1.hashCode()); result = prime * result + ((add5 == null) ? 0 : add5.hashCode()); result = prime * result + ((cellPhone == null) ? 0 : cellPhone.hashCode()); result = prime * result + ((department == null) ? 0 : department.hashCode()); result = prime * result + ((email == null) ? 0 : email.hashCode());

result = prime * result + ((fullName == null) ? 0 : fullName.hashCode()); result = prime * result + ((id == null) ? 0 : id.hashCode()); result = prime * result + ((login == null) ? 0 : login.hashCode()); result = prime * result + ((password == null) ? 0 : password.hashCode()); result = prime * result + (processed ? 1231 : 1237); result = prime * result + ((tel == null) ? 0 : tel.hashCode()); return result; } @Override publicboolean equals(Object obj) { if (this == obj) { returntrue; } if (obj == null) { returnfalse; } if (getClass() != obj.getClass()) { returnfalse; } User other = (User) obj; if (add1 == null) { if (other.add1 != null) { returnfalse; } } elseif (!add1.equals(other.add1)) { returnfalse; } if (add5 == null) { if (other.add5 != null) { returnfalse; } } elseif (!add5.equals(other.add5)) { returnfalse; } if (cellPhone == null) { if (other.cellPhone != null) { returnfalse; } } elseif (!cellPhone.equals(other.cellPhone)) { returnfalse; }

if (department == null) { if (other.department != null) { returnfalse; } } elseif (!department.equals(other.department)) { returnfalse; } if (email == null) { if (other.email != null) { returnfalse; } } elseif (!email.equals(other.email)) { returnfalse; } if (fullName == null) { if (other.fullName != null) { returnfalse; } } elseif (!fullName.equals(other.fullName)) { returnfalse; } if (id == null) { if (other.id != null) { returnfalse; } } elseif (!id.equals(other.id)) { returnfalse; } if (login == null) { if (other.login != null) { returnfalse; } } elseif (!login.equals(other.login)) { returnfalse; } if (password == null) { if (other.password != null) { returnfalse; } } elseif (!password.equals(other.password)) { returnfalse; } if (processed != other.processed) { returnfalse;

搜索更多关于: boss代码review记录 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

1.21.1 错误示例 1.21.2 Bug描述 Bug: its.sso.sync.rmc.pojo.User defines equals and uses Object.hashCode() Patternid: HE_EQUALS_USE_HASHCODE, type: HE, category: BAD_PRACTICE This class overrides equals(Object), but does not override hashCode(), and inherits the implementation of hashCode() from java.lang.Object (which returns the identity hash code, an arbitrary value as

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com