为什么哈希码不生成唯一的哈希码? [英] Why hashcode does not generate unique hashcode?

查看:371
本文介绍了为什么哈希码不生成唯一的哈希码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道hashCode()的默认实现在转换内部地址

We known that default implementation of hashCode() returns integer after converting the internal address converting the internal address of the object into an integer . So internal memory of every object is different, then why hashCode() does not generate unique hashcode.

我的问题是,如果我们不重写hashcode()并等于它,为什么hashcode()[返回整数即对象的地址表示形式]不会生成唯一代码?

My question is why hashcode() [It returns integer which is address representation of an object] does not generate unique code , if we don't override hashcode() and equals?

推荐答案

因为它不能.

由于只有2 ^ 32个不同的int,并且在任何VM实例中可能有2 ^ 32个以上的活动对象,因此从技术上讲,不可能为每个对象保证唯一的哈希码.

Since there are only 2^32 different ints and there may be more than 2^32 live objects in any VM instance, it is technically impossible to guarantee a unique hash code for each object.

即使默认哈希码可能是基于对象的内部地址的 ,它也不会与内部地址相同.

Even if the default hash code may be based on the internal address of the object, it is not identical to the internal address.

这篇关于为什么哈希码不生成唯一的哈希码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆