Object类的解释(equals,hashcode)
来源:互联网 发布:苹果网络信号差怎么办 编辑:程序博客网 时间:2024/05/16 10:20
public boolean equals(Object obj)
Indicates whether some other object is "equal to" this one.
The equals method implements an equivalence relation on non-null object references:
//自反性:如果非空引用,那么自己equals自己的话就返回true
It is reflexive: for any non-null reference value x, x.equals(x) should return true.
//对称性:x.equals(y) 那么y.equals(x)
It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
//传递性:x.equals(y) y.equals(z) 那么x.equals(z) 为true
It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
//一致性:x.equals(y)为true 那么x.equals(y) 第n次也应该为true 前题是x y 没有修改
It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
For any non-null reference value x, x.equals(null) should return false.
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
//我们要overwrite equals 方法 就要overwrite hashcode方法 因为有相同的equals方法就要有相同的hashcode
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
Parameters:
obj - the reference object with which to compare.
Returns:
true if this object is the same as the obj argument; false otherwise.
See Also:
hashCode(), Hashtable
public int hashCode()
Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.
The general contract of hashCode is:
//在java应用的执行过程中,对于同一个对象的hashcode方法的多次调用,他们应该返回同样的值(前提的中间没有变化)
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
//对于两个对象,如果使用equals方法返回true,那么这两个hashcode返回的值必须相同
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
//对于两个对象,如果使用equals方法比较返回false,那个这两个hashcode返回值可以相同可以不同,如果不相同,那么性能会好些。
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
Returns:
a hash code value for this object.
See Also:
equals(java.lang.Object), Hashtable
equals方法的源码
//实际就是地址相同的equals就相同
public boolean equals(Object obj) {
return (this == obj);
}
//对象名称+返回对象的地址(不一定是真实的内存地址)
public native int hashCode();
理解为什么重写了equals方法就要重写hashcode
我的理解是hashcode是用于散列数据的快速存取,如利用HashSet/HashMap/Hashtable类来存储数据时,都是根据存储对象的hashcode值来进行判断是否相同的。这样如果我们对一个对象重写了euqals,意思是只要对象的成员变量值都相等那么euqals就等于true,但不重写hashcode,那么我们再new一个新的对象,当原对象.equals(新对象)等于true时,两者的hashcode却是不一样的,由此将产生了理解的不一致,如在存储散列集合时(如Set类),将会存储了两个值一样的对象,导致混淆,因此,就也需要重写hashcode。为了保证这种一致性,必须满足以下两个条件:
(1)当obj1.equals(obj2)为true时,obj1.hashCode() == obj2.hashCode()必须为true
(2)当obj1.hashCode() == obj2.hashCode()为false时,obj1.equals(obj2)必须为false
- Object类的解释(equals,hashcode)
- Object类:hashCode() 与 equals() 的关系
- 关于Object类的equals()和hashcode()
- Object的equals()及hashCode()(
- Object 类 equals toString hashCode
- 关于Object类的equals方法和hashCode方法
- Object类中equals,hashCode和toString方法的学习
- Java源码学习-Object类的hashCode和equals方法
- 覆盖Object类的equals、hashCode和toString方法
- Object类中的toString(),equals(), hashCode()方法之间的关系
- Java中Object类的equals()和hashCode()方法
- 复写Object超类的equals,hashCode,toString方法
- Object类的equals方法和hashCode方法
- 整理 Object 类(为什么重写 equals 和 hashCode 方法)
- Object类中hashCode()和equals()方法详解(附图)
- java基础之Object类的三个方法(hashCode、toString、equals)
- 关于Object的equals()及hashCode()
- 关于Object的equals()及hashCode()
- WebService处理大数据量数据出错:运行配置文件中指定的扩展时出现异常。 ---> 超过了最大请求长度。.
- 最近烦透了。程序呀,程序
- winform c# 中获取CheckListBox 值
- 经典的C++库
- [Java] 查看java对象的内存占用情况--java 优化系列
- Object类的解释(equals,hashcode)
- The SAP Toolbar control
- 实战activeMQ
- 创建透明的窗体并移动它上面的子窗体
- android内核编译札记
- C#中Struct和Class区别
- 做sql2005的数据库镜像时遇到的问题:Microsoft SQL Server,错误: 1498
- 网站内容页优化排名分析
- 后台自动发送邮件的程序(Java Mail,WEB版)