在业务开发当中,HashMap与HashTable都是用key-value的形式来存储数据,并且都是使用hashing技术来存储key,但是也有许多的同步之处。
| HashMap | HashTable |
|---|---|
| 1.HashMap是非同步的,并且不是线程安全的 | HashTable是同步的,并且是线程安全的 |
| 2.HashMap允许一个null作为key,允许多个null作为value | HashTable不允许有null键或者null值 |
| 3.HashMap是在JDK1.2被引入的 | HashTable是一个遗留的类 |
| 4.HashMap是高效快速的 | HashTable因为有了同步机制,效率差很多 |
| 5.可以通过Map m = Collections.synchronizedMap(hashMap);来使用同步的HashMap | 本身就是同步的,但不能取消同步 |
| 6.HashMap使用Iterator迭代器遍历 | HashTable使用Enumerator和Iterator进行遍历 |
| 7.HashMap的Iterator是快速失败的 | HashTable的Enumerator不是快速失败的 |
| 8.HashMap继承自AbstractMap类 | HashTable继承自Dictionary类 |