File tree Expand file tree Collapse file tree 1 file changed +21
-1
lines changed
Expand file tree Collapse file tree 1 file changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -236,7 +236,27 @@ public class test1 {
236236面试官可能会问你:“你重写过 hashcode 和 equals 么,为什么重写equals时必须重写hashCode方法?”
237237
238238### hashCode()介绍
239- hashCode() 的作用是获取哈希码,也称为散列码;它实际上是返回一个int整数。这个哈希码的作用是确定该对象在哈希表中的索引位置。hashCode() 定义在JDK的Object.java中,这就意味着Java中的任何类都包含有hashCode() 函数。另外需要注意的是: Object 的 hashcode 方法是本地方法,也就是用 c 语言或 c++ 实现的,该方法直接返回对象的 内存地址。
239+ hashCode() 的作用是获取哈希码,也称为散列码;它实际上是返回一个int整数。这个哈希码的作用是确定该对象在哈希表中的索引位置。hashCode() 定义在JDK的Object.java中,这就意味着Java中的任何类都包含有hashCode() 函数。另外需要注意的是: Object 的 hashcode 方法是本地方法,也就是用 c 语言或 c++ 实现的,该方法通常用来将对象的 内存地址 转换为整数之后返回。
240+
241+ ``` java
242+ /**
243+ * Returns a hash code value for the object. This method is
244+ * supported for the benefit of hash tables such as those provided by
245+ * {@link java.util.HashMap}.
246+ * <p >
247+ * As much as is reasonably practical, the hashCode method defined by
248+ * class {@code Object } does return distinct integers for distinct
249+ * objects. (This is typically implemented by converting the internal
250+ * address of the object into an integer, but this implementation
251+ * technique is not required by the
252+ * Java&trade ; programming language.)
253+ *
254+ * @return a hash code value for this object.
255+ * @see java.lang.Object#equals(java.lang.Object)
256+ * @see java.lang.System#identityHashCode
257+ */
258+ public native int hashCode();
259+ ```
240260
241261散列表存储的是键值对(key-value),它的特点是:能根据“键”快速的检索出对应的“值”。这其中就利用到了散列码!(可以快速找到所需要的对象)
242262
You can’t perform that action at this time.
0 commit comments