hashcode相等时,对象内容是否相等?

答:不一定

为什么不一定呢?因为有一些不同的中文和字母在进行hashcode后会生成同样的hashcode

 

package com.lp.utils;

public class Test {
    public static void main(String[] args) {

        String s ="Ea";
        String d ="FB";
        System.out.println("字符串的哈希码为s :" + s.hashCode() );
        System.out.println("字符串的哈希码为d :" + d.hashCode() );

        StringBuilder s1 =new StringBuilder("Ea");
        StringBuilder d1 =new StringBuilder("FB");
        System.out.println("字符串的哈希码为s1 :" + s1.hashCode() );
        System.out.println("字符串的哈希码为d1 :" + d1.hashCode() );

        String s2 ="1";
        String d2 ="2";
        System.out.println("字符串的哈希码为s2 :" + s2.hashCode() );
        System.out.println("字符串的哈希码为d2 :" + d2.hashCode() );

    }

}

以上是演示代码,下面贴出控制台输出

可以看出s和d对象生成的hashcode是同样的2236,但是对象内容却不一样。

 

以上内容也是我在参考借鉴别人的例子,然后自己敲了一遍。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值