HashMap和Hashtable的区别

都属于Map接口的类,实现了将惟一键映射到特定的值上。

HashMap 类没有分类或者排序。它允许一个 null 键和多个 null 值。

Hashtable 类似于 HashMap,但是不允许 null 键和 null 值。它也比 HashMap 慢,因为它是同步的。

Hashtable继承自Dictionary类,而HashMap是Java1.2引进的Map interface的一个实现。

HashMap允许将null作为一个entry的key或者value,而Hashtable不允许,还有就是,HashMap把Hashtable的contains方法去掉了,改成containsvalue(Returns true if this map maps one or more keys to the specified value)和containsKey(Returns true if this map contains a mapping for the specified key)。因为contains(Tests if some key maps into the specified value in this hashtable)方法容易让人引起误解。

最大的不同是,Hashtable的方法是Synchronize的,而HashMap不是,在多个线程访问Hashtable时,不需要自己为它的方法实现同步,而HashMap 就必须为之提供外同步。

Hashtable和HashMap采用的hash/rehash算法都大概一样,所以性能不会有很大的差异。

 

posted @ 2017-08-28 22:06  Techno_geek  阅读(149)  评论(0编辑  收藏  举报