最简单的哈希算法,哈希算法通俗易懂

首页 > 经验 > 作者:YD1662022-11-18 03:08:01

测试

@Test public void test_hashMap03() { Map<String, String> map = new HashMap03ByOpenAddressing<>(); map.put("01", "花花"); map.put("05", "豆豆"); logger.info("碰撞前 key:{} value:{}", "01", map.get("01")); // 下标碰撞 map.put("09", "蛋蛋"); map.put("12", "苗苗"); logger.info("碰撞前 key:{} value:{}", "01", map.get("01")); }

最简单的哈希算法,哈希算法通俗易懂(9)

07:20:22.382 [main] INFO cn.bugstack.algorithms.test.AlgorithmsTest - 碰撞前 key:01 value:花花 07:20:22.387 [main] INFO cn.bugstack.algorithms.test.AlgorithmsTest - 碰撞前 key:01 value:花花 07:20:22.387 [main] INFO cn.bugstack.algorithms.test.AlgorithmsTest - 数据结构:HashMap{tab=[null,{"key":"01","value":"花花"},{"key":"09","value":"蛋蛋"},{"key":"12","value":"苗苗"},null,{"key":"05","value":"豆豆"},null,null]} Process finished with exit code 0

4. 合并散列

说明:合并散列是开放寻址和单独链接的混合,碰撞的节点在哈希表中链接。此算法适合固定分配内存的哈希桶,通过存放元素时识别哈希桶上的最大空槽位来解决合并哈希中的冲突。

public class HashMap04ByCoalescedHashing<K, V> implements Map<K, V> { private final Node<K, V>[] tab = new Node[8]; @Override public void put(K key, V value) { int idx = key.hashCode() & (tab.length - 1); if (tab[idx] == null) { tab[idx] = new Node<>(key, value); return; } int cursor = tab.length - 1; while (tab[cursor] != null && tab[cursor].key != key) { --cursor; } tab[cursor] = new Node<>(key, value); // 将碰撞节点指向这个新节点 while (tab[idx].idxOfNext != 0){ idx = tab[idx].idxOfNext; } tab[idx].idxOfNext = cursor; } @Override public V get(K key) { int idx = key.hashCode() & (tab.length - 1); while (tab[idx].key != key) { idx = tab[idx].idxOfNext; } return tab[idx].value; } static class Node<K, V> { final K key; V value; int idxOfNext; public Node(K key, V value) { this.key = key; this.value = value; } } }

最简单的哈希算法,哈希算法通俗易懂(10)

测试

07:18:43.613 [main] INFO cn.bugstack.algorithms.test.AlgorithmsTest - 碰撞前 key:01 value:花花 07:18:43.618 [main] INFO cn.bugstack.algorithms.test.AlgorithmsTest - 碰撞前 key:01 value:苗苗 07:18:43.619 [main] INFO cn.bugstack.algorithms.test.AlgorithmsTest - 数据结构:HashMap{tab=[null,{"idxOfNext":7,"key":"01","value":"花花"},null,null,null,{"idxOfNext":0,"key":"05","value":"豆豆"},{"idxOfNext":0,"key":"12","value":"苗苗"},{"idxOfNext":6,"key":"09","value":"蛋蛋"}]} Process finished with exit code 0

5. 杜鹃散列

说明:这个名字起的比较有意思,也代表着它的数据结构。杜鹃鸟在孵化的时候,雏鸟会将其他蛋或幼崽推出巢穴;类似的这个数据结构会使用2组key哈希表,将冲突元素推到另外一个key哈希表中。

private V put(K key, V value, boolean isRehash) { Object k = maskNull(key); if (containsKey(k)) { return null; } if (insertEntry(new Entry<K, V>((K) k, value))) { if (!isRehash) { size ; } return null; } rehash(2 * table.length); return put((K) k, value); } private boolean insertEntry(Entry<K, V> e) { int count = 0; Entry<K, V> current = e; int index = hash(hash1, current.key); while (current != e || count < table.length) { Entry<K, V> temp = table[index]; if (temp == null) { table[index] = current; return true; } table[index] = current; current = temp; if (index == hash(hash1, current.key)) { index = hash(hash2, current.key); } else { index = hash(hash1, current.key); } count; } return false; }

最简单的哈希算法,哈希算法通俗易懂(11)

测试

最简单的哈希算法,哈希算法通俗易懂(12)

上一页12345下一页

栏目热文

文档排行

本站推荐

Copyright © 2018 - 2021 www.yd166.com., All Rights Reserved.