equals(),hashCode()的区别和联系

1.首先讨论==问题:

看一组比较,包括基本类型和其包装类,引用类型之间的比较

String str1 = new String("hello");
String str2 = new String("hello");
System.out.println(str1==str2);//false

String str3 = "hello";
String str4 = "hello";
System.out.println(str3==str4);//true
System.out.println(str1==str3);//false;

Integer it1 = 12;
Integer it2 = 12;
System.out.println(it1==it1);//true

Integer it3 = new Integer(12);
Integer it4 = new Integer(12);
System.out.println(it3==it4);//false
System.out.println(it1==it3);//false

int it5 = 12;
System.out.println(it1==it5);//true
System.out.println(it3==it5);//true

==比较的是对象的引用,凡是与new的对象比较,必须是同一对象,特殊情况是基本数据类型和String的常量池,String常量池里面的值唯一,因此值相同,地址相同,==成立;基本数据类型和其包装类比较,会自动拆装箱,值相同==就成立

2.equals()方法

原生的equals()比较的也是引用地址,但是重写之后可以比较堆里面的值内容是否相等,一般用于集合元素的比较,避免重复插入。下面是重写的String的equals()方法

public boolean equals(Object anObject) {  
    if (this == anObject) {  //同一对象
        return true;  
    }  
    if (anObject instanceof String) {  
        String anotherString = (String)anObject;  
        int n = count;  
        if (n == anotherString.count) {  //长度相同
        char v1[] = value;  
        char v2[] = anotherString.value;  
        int i = offset;  
        int j = anotherString.offset;  
        while (n-- != 0) {  
            if (v1[i++] != v2[j++])  
            return false;  
        }  
        return true;  //每个对应位置的字符相等
        }  
    }  
    return false;  //长度不等 直接返回
    }  

3.hashCode()方法

根据对象的值计算其物理存储位置,即hash值,对于值equals的对象,其hashCode一定相等,但是即使值不相等,也可能得到相同的hash值,因此在同一hash值的位置形成了链表,可以看出,每次new出一个对象,直接计算出其hash值,然后寻找对应位置上,再使用equals方法比较,是否已存在值就可以知道对象是否重复了,这样比依次调用equals()与集合中的每个元素比较要节省很多时间。hash地址允许冲突,但是设计的越分散性能越好。

4.equals()与hashCode()的比较

两个对象equals,则hashCode一定相等,因为计算方式一样

两个对象不equals,则hashCode不一定不相等,因为可能存在地址冲突

两个对象hashCode相等,对象不一定equals,因为可能存在地址冲突

两个对象hashCode不相等,对象一定不equals

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值