Java实现哈希表

匿名 (未验证) 提交于 2019-12-02 21:38:03

一些概念:

1、哈希表:每一个字符都和一个索引相对应 O(1)的查找操作!
2、哈希函数:对于给定字符,将其转为索引的方法;
3、哈希表充分体现了算法设计领域的经典思想:空间换时间,哈希表是时间和空间之间的平衡
4、哈希函数的设计:”键“通过哈希函数得到的”索引“分布越均匀越好
5、哈希函数设计原则:

  • 一致性:如果a==b,则hash(a)= hash(b)
  • 高效性:计算高效简便
  • 均匀性:哈希值均匀分布

6、哈希表:均摊复杂度为O(1),牺牲了顺序性;
7、哈希冲突的处理方法:

  • 开放地址法:开放(每个地址对任何元素开放)
    线性探测(遇到哈希冲突+1)、平方探测(遇到哈希冲突 + 1+ 4 + 9 + 16)、二次哈希(遇到哈希冲突 + hash2(key))
    负载率:哈希表存储的元素的总数占整个地址的百分比
  • 链地址法:封闭
  • 再哈希法:用另外哈希函数找索引
  • Coalesced Hashing:综合了Separate Chaining 和 Open Addressing

Java实现哈希表:

import java.util.TreeMap;  public class HashTable<K, V> {     private final int[] capacity             = {53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593,             49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469,             12582917, 25165843, 50331653, 100663319, 201326611, 402653189, 805306457, 1610612741};//保证扩容缩容是素数      private static final int upperTol = 10;     private static final int lowerTol = 2;     private int capacityIndex = 0;      private TreeMap<K, V>[] hashtable;     private int M;     private int size;      public HashTable() {         this.M = capacity[capacityIndex];         size = 0;         hashtable = new TreeMap[M];         for(int i = 0; i < M; i++) {             hashtable[i] = new TreeMap<>();         }     }       private int hash(K key) {         return (key.hashCode() & 0x7fffffff) % M;     }      public int getSize() {         return size;     }      public void add(K key, V value) {         TreeMap<K, V> map = hashtable[hash(key)];         if(map.containsKey(key)) {             map.put(key, value);         } else {             map.put(key, value);             size++;              if(size >= upperTol * M && capacityIndex + 1 < capacity.length) {                 capacityIndex++;                 resize(capacity[capacityIndex]);             }         }     }      public V remove(K key) {         TreeMap<K, V> map = hashtable[hash(key)];         V ret = null;         if(map.containsKey(key)) {             ret = map.remove(key);             size--;              if(size < lowerTol * M && capacityIndex - 1 >= 0) {                 capacityIndex--;                 resize(capacity[capacityIndex]);             }         }         return ret;     }      public void set(K key, V value) {         TreeMap<K, V> map = hashtable[hash(key)];         if(!map.containsKey(key)) {             throw new IllegalArgumentException(key + " does't exist!");         }         map.put(key, value);     }      public boolean contains(K key) {         return hashtable[hash(key)].containsKey(key);     }      public V get(K key) {         return hashtable[hash(key)].get(key);     }      private void resize(int newM) {         TreeMap<K, V>[] newHashTable = new TreeMap[newM];         for(int i =0; i < newM; i++) {             newHashTable[i] = new TreeMap<>();         }          int oldM = M;         this.M = newM;         for(int i = 0; i < oldM; i++) {             TreeMap<K, V> map = hashtable[i];             for(K key : map.keySet()) {                 newHashTable[hash(key)].put(key, map.get(key));             }         }          this.hashtable = newHashTable;     }  } 
文章来源: https://blog.csdn.net/weixin_40617102/article/details/90411773
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!