重写equals()方法同时还需要重写hashCode()方法
来源:互联网 发布:华中科技大学网络大专 编辑:程序博客网 时间:2024/05/22 00:16
object对象中的 public boolean equals(Object obj),对于任何非空引用值 x 和 y,当且仅当 x 和 y 引用同一个对象时,此方法才返回 true;
**注意:当此方法被重写时,通常有必要重写 hashCode 方法,以维护 hashCode 方法的常规协定,该协定声明相等对象必须具有相等的哈希码。如下:
(1)当obj1.equals(obj2)为true时,obj1.hashCode() == obj2.hashCode()必须为true
(2)当obj1.hashCode() == obj2.hashCode()为false时,obj1.equals(obj2)必须为false**
如果不重写equals,那么比较的将是对象的引用是否指向同一块内存地址,重写之后目的是为了比较两个对象的value值是否相等。特别指出利用equals比较八大包装对象
(如int,float等)和String类(因为该类已重写了equals和hashcode方法)对象时,默认比较的是值,在比较其它自定义对象时都是比较的引用地址
hashcode是用于散列数据的快速存取,如利用HashSet/HashMap/Hashtable类来存储数据时,都是根据存储对象的hashcode值来进行判断是否相同的。
这样如果我们对一个对象重写了euqals,意思是只要对象的成员变量值都相等那么euqals就等于true,但不重写hashcode,那么我们再new一个新的对象,
当原对象.equals(新对象)等于true时,两者的hashcode却是不一样的,由此将产生了理解的不一致,如在存储散列集合时(如Set类),将会存储了两个值一样的对象,
导致混淆,因此,就也需要重写hashcode()。
下面写个例子说明:
public class User { private String name; private String age; private String weight; public User(String name,String age,String weight) { this.name = name; this.age = age; this.weight = weight; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getWeight() { return weight; } public void setWeight(String weight) { this.weight = weight; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((age == null) ? 0 : age.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((weight == null) ? 0 : weight.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; User other = (User) obj; if (age == null) { if (other.age != null) return false; } else if (!age.equals(other.age)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (weight == null) { if (other.weight != null) return false; } else if (!weight.equals(other.weight)) return false; return true; } @Override public String toString() { return "User [name=" + name + ", age=" + age + ", weight=" + weight + "]"; }}
public class Test { public static void main(String[] args) { User u1 = new User("张三","12","45"); User u2 = new User("李四", "13", "46"); User u3 = new User("张三","12","45"); Collection c = new HashSet(); c.add(u1); c.add(u2); c.add(u3); System.out.println(u1.equals(u2)); System.out.println(u1.equals(u3)); System.out.println(u1.hashCode()); System.out.println(u2.hashCode()); System.out.println(u3.hashCode()); System.out.println(c); }}
重写equals()方法和hashCode()方法之后的结果:
只重写equals()方法的结果:
第一张图的结果分析,debug执行程序的时候发现,每次执行c.add()方法都需要调用hashCode()方法生成一个哈希码存入对象,第二次调用也是生成哈希码同时拿着这个哈希码去遍历集合比较是否有相同的哈希码,如果有则返回。参考下图的jdk源码红线处,c.add()方法最终调用了HashMap.put()方法。
查看没有重写hashCode()方法的执行结果发现,如果没有重写这个方法,生成的哈希码每次都是不同的,也就是说每创建一个对象都是重新生成一个哈希码,这样即使两个对象的值相等,也是返回false。
- 重写equals()方法同时还需要重写hashCode()方法
- 为什么equals()方法和hashCode()方法需要同时重写?
- 在重写了对象的equals方法后,还需要重写hashCode方法吗?
- 在重写了对象的equals方法后,还需要重写hashCode方法吗?
- 在重写了对象的equals方法后,还需要重写hashCode方法吗?
- 重写equals需要同时重写hashCode()
- 重写equals方法的同时为什么要重写hashcode方法
- 重写对象的equals(Object)方法,同时重写Hashcode()方法
- 重写equals()方法的同时要重写hashcode()方法
- why在重写equals时还必须重写hashcode方法
- why在重写equals时还必须重写hashcode方法
- 重写equals时还必须重写hashcode方法,详解
- 重写equals时还必须重写hashcode方法,详解
- 重写equals时还必须重写hashCode方法
- 重写equals方法时为什么需要重写hashCode方法
- 重写equals方法之后,是否需要重写hashCode方法
- 重写equals方法一般需要重写hashCode方法的原因
- 重写equals方法和hashcode(集合需要重写)
- [1295]螺旋方阵 SDUTOJ-螺旋方阵
- Kafka简单示例
- webrtc中的网络反馈与控制【转】
- 蓝桥真题 加法变乘法
- 二维数组中查找指定数据
- 重写equals()方法同时还需要重写hashCode()方法
- QuickHit
- IIS7 CGI 的设置 -- IIS 下运行 Delphi 编译的 CGI 程序
- 使用apidoc生成接口文档
- 剑指offer_1 cpp
- Linux下java调取命令通过ip获取mac地址
- Shell入门(三)echo.printf.test命令
- Springmvc下一次选多个文件上传(二)Ajax异步
- springMVC下无法加载静态资源文件(js、css、image)