关于Java中的HashMap
生活随笔
收集整理的這篇文章主要介紹了
关于Java中的HashMap
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 容量總是2次冪
/*** Returns a power of two size for the given target capacity.*/static final int tableSizeFor(int cap) {int n = cap - 1;n |= n >>> 1;n |= n >>> 2;n |= n >>> 4;n |= n >>> 8;n |= n >>> 16;return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;}2 散列函數的設計會同時使用hashcode的高位和低位特性
static final int hash(Object key) {int h;return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16) ;}3 查找index位置 a%b=a&(b-1)
int index = hash(key) &(capacity-1)4 沖突解決方法是:鏈表法。在同一列表中元素個數大于8 ,使用紅黑樹。
總結
以上是生活随笔為你收集整理的关于Java中的HashMap的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Unity3D的断点调试功能
- 下一篇: Leetcode Top100题目和答案