Hashtable 与 HashMap 有什么不同之处?

今天楼主继续分享一道经典的Java面试题:


客官:小二,上题目;

小二:好勒,马上到(滑稽)


Hashtable 与 HashMap 有什么不同之处?
这两个类有许多不同的地方,下面列出了一部分:
a) Hashtable 是 JDK 1 遗留下来的类,而 HashMap 是后来增加的。
b)Hashtable 是同步的,比较慢,但 HashMap 没有同步策略,所以会更快。
c)Hashtable 不允许有个空的 key和value,但是 HashMap 允许出现一个 null key和value。


出于参考答案过于简单,于是楼主进行了这一步的探索,得到了这些知识分享给大家:


HashTable是什么?哈希表(Hashtable)又称为“散置”,Hashtable是会根据索引键的哈希程序代码组织成的索引键(Key)和值(Value)配对的集合。Hashtable 对象是由包含集合中元素的哈希桶(Bucket)所组成的。而Bucket是Hashtable内元素的虚拟子群组,可以让大部分集合中的搜寻和获取工作更容易、更快速。

HashMap是什么? 看另外一篇文章。


HashMap和HashTable的主要6个区别:

  1. HashMap是非线程同步的,HashTable是线程同步的。
  2. HashMap允许null作为键或者值,HashTable不允许
  3. HashTable中有个一个contains方法,HashMap去掉了此方法,但是HashMap提供了containsvalue和containsKey。因为contains方法容易让人引起误解。
  4. 效率上来讲,HashMap因为是非线程安全的,因此效率比HashTable高
  5. hashTable继承Dictionary,而HashMap继承Abstract

  1. public class HashMap<K,V>  
  2.     extends AbstractMap<K,V>  
  3.     implements Map<K,V>, Cloneable, Serializable  
  4. public class Hashtable<K,V>  
  5.     extends Dictionary<K,V>  
  6.     implements Map<K,V>, Cloneable, java.io.Serializable  

6.hashTable的put方法实现了同步,而hashMap没有

hashMap的put、get方法源码:


  1. public V put(K key, V value) {  
  2.         if (key == null)  
  3.             return putForNullKey(value);  
  4.         int hash = hash(key.hashCode());  
  5.         int i = indexFor(hash, table.length);  
  6.         for (Entry<K,V> e = table[i]; e != null; e = e.next) {  
  7.             Object k;  
  8.             if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {  
  9.                 V oldValue = e.value;  
  10.                 e.value = value;  
  11.                 e.recordAccess(this);  
  12.                 return oldValue;  
  13.             }  
  14.         }  
  15.   
  16.         modCount++;  
  17.         addEntry(hash, key, value, i);  
  18.         return null;  
  19.     }  
  20. public V get(Object key) {  
  21.         if (key == null)  
  22.             return getForNullKey();  
  23.         int hash = hash(key.hashCode());  
  24.         for (Entry<K,V> e = table[indexFor(hash, table.length)];  
  25.              e != null;  
  26.              e = e.next) {  
  27.             Object k;  
  28.             if (e.hash == hash && ((k = e.key) == key || key.equals(k)))  
  29.                 return e.value;  
  30.         }  
  31.         return null;  
  32.     }  

hashTable的put()、get()方法源码:

  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. public synchronized V get(Object key) {  
  35.     Entry tab[] = table;  
  36.     int hash = key.hashCode();  
  37.     int index = (hash & 0x7FFFFFFF) % tab.length;  
  38.     for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {  
  39.         if ((e.hash == hash) && e.key.equals(key)) {  
  40.         return e.value;  
  41.         }  
  42.     }  
  43.     return null;  
  44.     }  


从源码中可以看出hashTable实现了synchronized,并不允许null作为键值。



资料来源:http://my.oschina.net/mrku/blog/737808

          http://blog.csdn.net/caisini_vc/article/details/52450162

              



  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

弗兰随风小欢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值