java的hashCode()方法如何工作? [英] how does the hashCode() method of java works?

查看:132
本文介绍了java的hashCode()方法如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇java如何通过使用 hashCode()方法?

I am curious how java generates hash values by using hashCode() method of the Object API ?

推荐答案

Java不生成hashCode(),即没有任何自动发生在这里。但是, Object 会根据对象实例的内存地址生成一个HashCode。大多数类(特别是如果你打算在任何 Collection API中使用它)应该实现它们自己的HashCode(并通过契约它们自己的equals方法)。

Java doesn't generate hashCode(), i.e. nothing automatic is happening here. However, Object generates a HashCode based on the memory address of the instance of the object. Most classes (especially if you are going to use it in any of the Collection API) should implement their own HashCode (and by contract their own equals method).

这篇关于java的hashCode()方法如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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