Skip to content

Commit 2f61492

Browse files
authored
hashcode方法讲解的完善
1 parent 69c53d0 commit 2f61492

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

面试必备/最最最常见的Java面试题总结/第一周(2018-8-7).md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)