最近看HashMap的源码,其中雷同下标容易产生hash抵触,然而调试须要产生hash抵触,本文模仿hash抵触。

hash抵触原理

HashMap抵触是key首先调用hash()办法:

static final int hash(Object key) {    int h;    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);}

而后应用hash值和tab数组长度做与操作:

 (n - 1) & hash

算进去的下标,如果统一就会产生抵触。

通过ASKII码获取单个字符

开始想到单字符,比方a、b、c、d、e这类字符,然而如果一个一个试的话特地繁琐,想到了ASKII码:

遍历1~100ASKII码。通过ASKII码获取单字符:

for (int i = 33; i < 100; i++) {    char ch = (char) i;    String str = String.valueOf(ch);}

通过str获取下标,HashMap默认长度为16,所以n-1为15:

int index = 15 & hash(str);

获取产生hash抵触的字符

算出index统一的话,就放在一个列表中。不同的index放在HashMap中,残缺代码如下:

Map<Integer, List<String>> param = new HashMap<>();for (int i = 33; i < 100; i++) {    char ch = (char) i;    String str = String.valueOf(ch);    int index = 15 & hash(str);    List<String> list = param.get(index);    if (list == null) {        list = new ArrayList<>();    }    list.add(str);    param.put(index,list);}param.forEach((k,v) -> System.out.println(k + " " + Arrays.toString(v.toArray())));

输入后果:

0 [0, @, P, `]1 [!, 1, A, Q, a]2 [", 2, B, R, b]3 [#, 3, C, S, c]4 [$, 4, D, T]5 [%, 5, E, U]6 [&, 6, F, V]7 [', 7, G, W]8 [(, 8, H, X]9 [), 9, I, Y]

源码调试

依据下面算进去的后果,应用其中的一个例子:

1 [!, 1, A, Q, a]

先增加数据:

 Map<String,Integer> map = new HashMap<>(); map.put("!",1); map.put("1",1); map.put("A",1);

先增加1, A, Q三个数据。而后增加Q

关上调式,定位到putVal办法:

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,                   boolean evict) {    Node<K,V>[] tab; Node<K,V> p; int n, i;    if ((tab = table) == null || (n = tab.length) == 0)        n = (tab = resize()).length;    if ((p = tab[i = (n - 1) & hash]) == null)        tab[i] = newNode(hash, key, value, null);    else {        Node<K,V> e; K k;        if (p.hash == hash &&            ((k = p.key) == key || (key != null && key.equals(k))))            e = p;        else if (p instanceof TreeNode)            e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);        else {            for (int binCount = 0; ; ++binCount) {                if ((e = p.next) == null) {                    p.next = newNode(hash, key, value, null);                    if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st                        treeifyBin(tab, hash);                    break;                }                if (e.hash == hash &&                    ((k = e.key) == key || (key != null && key.equals(k))))                    break;                p = e;            }        }        if (e != null) { // existing mapping for key            V oldValue = e.value;            if (!onlyIfAbsent || oldValue == null)                e.value = value;            afterNodeAccess(e);            return oldValue;        }    }    ++modCount;    if (++size > threshold)        resize();    afterNodeInsertion(evict);    return null;}

在源码解析文章详解HashMap源码解析(下)中晓得,产生hash抵触是会在下面代码的第16行,始终for循环遍历链表,替换雷同的key或者在链表中增加数据:

for (int binCount = 0; ; ++binCount) {    if ((e = p.next) == null) {        p.next = newNode(hash, key, value, null);        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st            treeifyBin(tab, hash);        break;    }    if (e.hash == hash &&        ((k = e.key) == key || (key != null && key.equals(k))))        break;    p = e;}

调式:

会始终遍历for循环,直到p.next==null遍历到链尾,而后在链表尾部增加节点数据:

p.next = newNode(hash, key, value, null);

总结

  • 通过(h = key.hashCode()) ^ (h >>> 16)高位运算hash码(n - 1) & hash哈希表数组长度取模,剖析hash抵触原理。
  • 通过ASKII码遍历获取字符串,获取产生hash抵触的字符。
  • 调用put办法,调用hash抵触源码。