【java编程】重写HashCode和equals方法
2024-08-20 06:22:10
【一】重写equals方案的规则
equals方法本来的原则
1、类的每个实例本质上都是唯一的。
2、不关心类是否提供了“逻辑相等”的测试功能
3、超类已经覆盖了equals,从超类继承过来的行为对于子类也是合适的。
4、类是自有的活是包级私有的,可以确定equals方法永远不会被调用。这个时候就要重写,防止被外界调用。
equals方法实现了等价关系。
1、自反性:对于任何非null的引用值x.则必须满足x.equals(x)必须返回true
2、对称性:对于任何非null的引用x,y. x.equals(y)返回true,则y.equals(x)必须返回true
3、传递性:对于任何非null的引用x,y,z。则x.equals(y)返回true,y.equals(z)返回true.则x.equals(z)必须返回true
4、一致性:对于任何非null的引用x,y。只要对象里的信息没有被修改。x.equals(y)多次调用,要么一直返回true,要么一直返回false
5、对于任何非null的引用值x.x.equals(null)必须返回false
【二】重写hashCode方法的规则
覆盖了equals时,总要覆盖hashCode方法。
一个重写hashCode和equals的实例
import org.apache.commons.lang3.StringUtils; /**
*
*/
public final class Category {
private volatile int hashCode = 0;
private final boolean nullOrEmptyIsCached;
private final int initialCapacity;
private final long maxSize;
private final long timeOut;
private final String cacheKey; public Category(int initialCapacity, long maxSize, long timeOut, String cacheKey, boolean nullOrEmptyIsCached) {
this.initialCapacity = initialCapacity;
this.maxSize = maxSize;
this.timeOut = timeOut;
this.cacheKey = cacheKey;
this.nullOrEmptyIsCached = nullOrEmptyIsCached;
} @Override
public int hashCode() {
int internalHashCode = hashCode;
if (internalHashCode == 0) {
int nullOrEmptyIsCachedInt = nullOrEmptyIsCached ? 1 : 0;
internalHashCode = 31 * internalHashCode + nullOrEmptyIsCachedInt;
internalHashCode = 31 * internalHashCode + initialCapacity;
int mS = (int) (maxSize ^ (maxSize >>> 32));
internalHashCode = 31 * internalHashCode + mS;
int tO = (int) (timeOut ^ (timeOut >>> 32));
internalHashCode = 31 * internalHashCode + tO;
if (StringUtils.isNotEmpty(cacheKey)) {
internalHashCode = 31 * internalHashCode + cacheKey.hashCode();
}
hashCode = internalHashCode;
}
return internalHashCode;
} @Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Category)) {
return false;
}
Category otherCategory = (Category) obj;
return this.nullOrEmptyIsCached == otherCategory.nullOrEmptyIsCached && this.initialCapacity == otherCategory.initialCapacity && this.maxSize == otherCategory.maxSize && this.timeOut == otherCategory.timeOut && (this.cacheKey == null ? this.cacheKey == otherCategory.cacheKey : this.cacheKey.equals(otherCategory.cacheKey));
}
//省略sett,gett
}
最新文章
- System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本问题
- IntelliJ IDEA - 热部署插件JRebel 安装使用教程
- 【转】一个lucene的官网例子
- Base64 字符串转图片 问题整理汇总
- WebStorm设置编辑器中的字体大小
- IOS第17天(2,Quartz2D图片剪裁变圆行图,和截屏图片)
- dojo grid 分页
- readint writeint
- phaser源码解析(三) Phaser.Utils类下isPlainObject方法
- 细谈sass和less中的变量及其作用域
- Unity非运行模式下实现动画播放/回退工具
- 开源的JavaScript插件化框架MinimaJS
- C# 处理Word自动生成报告 二、数据源例子
- 51Nod 1016 水仙花数 V2(组合数学,枚举打表法)
- 类 Random
- 15Linux_DHCP_Postfix_Dovecot_LDAP
- enctype=";multipart/form-data";表单传值问题
- 在Windows Server 2008R2中部署 AspNetCore
- Sublime for mac 开发Golang : 一步步环境配置
- springboot学习过程笔记
热门文章
- dns未设置 PHP Warning: file_get_contents():php_network_getaddresses: getaddrinfo failed:
- new JSONObject(str)无法解析 报错:org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
- 雷林鹏分享:C# 正则表达式
- javaScript数组的三种属性—数组索引、数组内置属性、数组自定义属性
- python-day68--模型层基础(model)
- mongodb的capped Collection集合
- Winform中用comboBox来选择显示Dataset中表格数据
- 快速切题 sgu123. The sum
- 跟我一起学习ASP.NET 4.5 MVC4.0(四)
- Alphabet Cookies