equals && hashCode

关于变量之间的比较,可以分为基础类型变量的比较和对象之间的比较。

1、对于基本类型来说,他们存储在jvm的栈中,因此比较的是变量的内容,也就是比较的变量的值。

2、对于引用类型来说,因为对象内容存储在jvm的堆中,栈中只是存储对象的引用(地址),==比较的都是栈中的内容,即对象的引用,也就是比较的是两个对象的地址。而object中equals是用==来实现的,如果未重写equals方法,那么实际比较的仍然是对象的地址。

但是String等常用类已经重写了object中的equals方法,让equals来比较实际内容。

object中equals与hashCode方法介绍如下:

hashCode

Returns a hash code value for the object.
This method is supported for the benefit of hash tables such as those provided by HashMap.
返回对象的哈希码值。支持此方法是为了提高诸如java.util.HashMap 提供的哈希表的性能。

The general contract of hashCode is:
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.
在 Java 应用程序执行期间,在对同一对象多次调用 hashCode 方法时,必须一致地返回相同的整数,前提是将对象进行 equals 比较时所用的信息没有被修改。从某一应用程序的一次执行到同一应用程序的另一次执行,该整数无需保持一致。 

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(Object) 方法,两个对象是相等的,那么对这两个对象中的每个对象调用 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 hash tables.
如果根据 equals(java.lang.Object) 方法,两个对象不相等,那么对这两个对象中的任一对象上调用 hashCode 方法不 要求一定生成不同的整数结果。但是,程序员应该意识到,为不相等的对象生成不同整数结果可以提高哈希表的性能。 

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.)
实际上,由 Object 类定义的 hashCode 方法确实会针对不同的对象返回不同的整数。(这一般是通过将该对象的内部地址转换成一个整数来实现的,但是 JavaTM 编程语言不需要这种实现技巧。) 

equals:

The equals method implements an equivalence relation 
on non-null object references:
equals 方法在非空对象引用上实现相等关系: 

It is reflexive: for any non-null reference value x, x.equals(x) should return true.
自反性:对于任何非空引用值 x,x.equals(x) 都应返回 true。 

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 和 y,当且仅当 y.equals(x) 返回 true 时,x.equals(y) 才应返回 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、y 和 z,如果 x.equals(y) 返回 true,并且 y.equals(z) 返回 true,那么 x.equals(z) 应返回 true。 

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.
一致性:对于任何非空引用值 x 和 y,多次调用 x.equals(y) 始终返回 true 或始终返回 false,前提是对象上 equals 比较中所用的信息没有被修改。 

For any non-null reference value  x, x.equals(null) should return false.
对于任何非空引用值 x,x.equals(null) 都应返回 false。 

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.
注意:当此方法被重写时,通常有必要重写 hashCode 方法,以维护 hashCode 方法的常规协定,该协定声明***相等对象必须具有相等的哈希码***。 

总结如下
1) 对象相等则hashCode一定相等;
2) hashCode相等对象未必相等。

注意:
1) 因为 Set 存储的是不重复的对象,依据 hashCode 和 equals 进行判断,所以 Set 存储的
对象必须重写这两个方法。
2) 如果自定义对象作为 Map 的键,那么必须重写 hashCode 和 equals。

示例:

class Person implements Serializable{
    private String no
    private String name

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Person)) return false;

        Person that = (Person) o;

        if (no != null ? !no.equals(that.no) : that.no != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = no != null ? no.hashCode() : 0;
        //result = 31 * result + (name != null ? name.hashCode() : 0);对象相等则hashCode一定相等;
        return result;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值