Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4e647d5

Browse files
增加了对 hash方法的理解
1 parent e03408d commit 4e647d5

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

‎Java相关/这几道Java集合框架面试题几乎必问.md‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,39 @@ Arraylist不是同步的,所以在不需要保证线程安全时时建议使
3939

4040
### JDK1.8之前
4141

42-
JDK1.8 之前 HashMap **数组+链表** 组成的(**"链表散列"** 即数组和链表的结合体),数组是 HashMap 的主体,链表则是主要为了解决哈希冲突而存在的(HashMap 采用 **"拉链法也就是链地址法"** 解决冲突),如果定位到的数组位置不含链表(当前 entrynext 指向 null ),那么对于查找,添加等操作很快,仅需一次寻址即可;如果定位到的数组包含链表,对于添加操作,其时间复杂度依然为 O(1),因为最新的 Entry 会插入链表头部,急需要简单改变引用链即可,而对于查找操作来讲,此时就需要遍历链表,然后通过 key 对象的 equals 方法逐一比对查找.
42+
JDK1.8 之前 HashMap 底层是 **数组和链表** 结合在一起使用也就是 **链表散列****HashMap 通过 keyhashCode 经过扰动函数处理过后得到 hash 值,当 hash 值相同时,通过拉链法解决冲突。**
4343

44-
> 所谓 **"拉链法"** 就是将链表和数组相结合。也就是说创建一个链表数组,数组中每一格就是一个链表。若遇到哈希冲突,则将冲突的值加到链表中即可。
44+
**所谓扰动函数指的就是 HashMap 的 hash 方法。使用 hash 方法也就是扰动函数是为了防止一些实现比较差的 hashCode() 方法 换句话说使用扰动函数之后可以减少碰撞。**
45+
46+
**JDK 1.8 HashMap 的 hash 方法源码:**
47+
48+
JDK 1.8 的 hash方法 相比于 JDK 1.7 hash 方法更加简化,但是原理不变。
49+
50+
```java
51+
static final int hash(Object key) {
52+
int h;
53+
// key.hashCode():返回散列值也就是hashcode
54+
// ^ :按位异或
55+
// >>>:无符号右移,忽略符号位,空位都以0补齐
56+
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
57+
}
58+
```
59+
对比一下 JDK1.7的 HashMap 的 hash 方法源码.
60+
61+
```java
62+
static int hash(int h) {
63+
// This function ensures that hashCodes that differ only by
64+
// constant multiples at each bit position have a bounded
65+
// number of collisions (approximately 8 at default load factor).
66+
67+
h ^= (h >>> 20) ^ (h >>> 12);
68+
return h ^ (h >>> 7) ^ (h >>> 4);
69+
}
70+
```
71+
72+
相比于 JDK1.8 的 hash 方法 ,JDK 1.7 的 hash 方法的性能会稍差一点点,因为毕竟扰动了 4 次。
73+
74+
所谓 **"拉链法"** 就是:将链表和数组相结合。也就是说创建一个链表数组,数组中每一格就是一个链表。若遇到哈希冲突,则将冲突的值加到链表中即可。
4575

4676

4777

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /