Hashtable 源码分析

本文详细介绍了Hashtable类的实现原理,包括其存储结构、构造函数、put和get方法的具体实现过程,并对比了Hashtable与HashMap的主要区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://frankfan915.iteye.com/blog/1152091

hashtable实现了Map接口. 

存储结构:类似于hashmap 
Java代码   收藏代码
  1. private transient Entry[] table;  

构造函数:类似于hashmap 
Java代码   收藏代码
  1.  public Hashtable(int initialCapacity, float loadFactor) {  
  2. if (initialCapacity < 0)  
  3.     throw new IllegalArgumentException("Illegal Capacity: "+  
  4.                                               initialCapacity);  
  5.        if (loadFactor <= 0 || Float.isNaN(loadFactor))  
  6.            throw new IllegalArgumentException("Illegal Load: "+loadFactor);  
  7.   
  8.        if (initialCapacity==0)  
  9.            initialCapacity = 1;  
  10. this.loadFactor = loadFactor;  
  11. table = new Entry[initialCapacity];  
  12. threshold = (int)(initialCapacity * loadFactor);  
  13.    }  

put方法:类似于hashmap 
Java代码   收藏代码
  1. public synchronized V put(K key, V value) {  
  2.     // Make sure the value is not null  
  3.     if (value == null) {  
  4.         throw new NullPointerException();  
  5.     }  
  6.   
  7.     // Makes sure the key is not already in the hashtable.  
  8.     Entry tab[] = table;  
  9.     int hash = key.hashCode();  
  10.     int index = (hash & 0x7FFFFFFF) % tab.length;  
  11.     for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {  
  12.         if ((e.hash == hash) && e.key.equals(key)) {  
  13.         V old = e.value;  
  14.         e.value = value;  
  15.         return old;  
  16.         }  
  17.     }  
  18.   
  19.     modCount++;  
  20.     if (count >= threshold) {  
  21.         // Rehash the table if the threshold is exceeded  
  22.         rehash();  
  23.   
  24.             tab = table;  
  25.             index = (hash & 0x7FFFFFFF) % tab.length;  
  26.     }  
  27.   
  28.     // Creates the new entry.  
  29.     Entry<K,V> e = tab[index];  
  30.     tab[index] = new Entry<K,V>(hash, key, value, e);  
  31.     count++;  
  32.     return null;  
  33. }  
  34.  protected void rehash() {  
  35.     int oldCapacity = table.length;  
  36.     Entry[] oldMap = table;  
  37.   
  38.     int newCapacity = oldCapacity * 2 + 1;  
  39.     Entry[] newMap = new Entry[newCapacity];  
  40.   
  41.     modCount++;  
  42.     threshold = (int)(newCapacity * loadFactor);  
  43.     table = newMap;  
  44.   
  45.     for (int i = oldCapacity ; i-- > 0 ;) {  
  46.         for (Entry<K,V> old = oldMap[i] ; old != null ; ) {  
  47.         Entry<K,V> e = old;  
  48.         old = old.next;  
  49.   
  50.         int index = (e.hash & 0x7FFFFFFF) % newCapacity;  
  51.         e.next = newMap[index];  
  52.         newMap[index] = e;  
  53.         }  
  54.     }  
  55.     }  

get 方法:类似于hashmap 
Java代码   收藏代码
  1.    public synchronized V get(Object key) {  
  2. Entry tab[] = table;  
  3. int hash = key.hashCode();  
  4. int index = (hash & 0x7FFFFFFF) % tab.length;  
  5. for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {  
  6.     if ((e.hash == hash) && e.key.equals(key)) {  
  7.     return e.value;  
  8.     }  
  9. }  
  10. return null;  
  11.    }  


总结:hashtable和hashmap的区别是: hashtable是线程安全的,hashmap不是线程安全的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值