The difference between HashMap and HashTable

1. 繼承和實現區別
   Hashtable是基於陳舊的Dictionary類,完成了Map接口, which is now obsolete in Jdk 1.7;HashMap是Java 1.2引進的Map接口的一個實現(HashMap繼承於AbstractMap,AbstractMap完成了Map接口)。
2. synchronization or thread-safe
   HashTable的方法是同步的,HashMap是未同步,因此在多線程場合要手動同步HashMap。
3. Null keys and null values
   HashTable不容許null值(key和value都不能夠),HashMap容許null值(key和value均可以)。
4. Iterating the values
   Hashmap使用iterator. HashTable is the only class other than vector which uses enumerator to iterate the values of HashTable object.
5. size
   HashTable中hash數組默認大小是11,增長的方式是 old*2+1。HashMap中hash數組的默認大小是16,並且必定是2的指數。
6. Fail-fast iterator
   The iterator in Hashmap is fail-fast iterator while the enumerator for Hashtable is not.
7. Performance
   Hashmap is much faster and uses less memory than Hashtable as former is unsynchronized . Unsynchronized objects are often much better in performance in compare to synchronized object like Hashtable in single threaded environment.
數組

相關文章
相關標籤/搜索