的区别
导读:
1 HashMap 不是线程安全的
hastmap 是一个接口 是 map 接口的子接口,是将键映射到值的对象,其中键和
值都是对象,并且不能包含重复键,但可以包含重复值。HashMap 允许 null key
和 null value,而 hashtable 不允许。
2 HashTable 是线程安全的一个 Collection。
HashMap 是 Hashtable 的轻量级实现(非线程安全的实现),他们都完成了 Map
接口,主要区别在于 HashMap 允许空(null)键值(key),由于非线程安全,效
率上可能高于 Hashtable。
HashMap 允许将 null 作为一个 entry 的 key 或者 value,而 Hashtable 不允许。
HashMap 把 Hashtable 的 contains 方法去掉了,改成 containsvalue 和
containsKey。因为 contains 方法容易让人引起误解。
Hashtable 继承自 Dictionary 类,而 HashMap 是 Java1.2 引进的 Map interface 的一
个实现。
最大的不同是,Hashtable 的方法是 Synchronize 的,而 HashMap 不是,在多个
线程访问 Hashtable 时,不需要自己为它的方法实现同步,而 HashMap 就必须为
之提供外同步。
Hashtable 和 HashMap 采用的 hash/rehash 算法都大概一样,所以性能不会有很大
的差异。
public static void main(String args[])
{
HashTable h=new HashTable();
h.put("用户 1",new Integer(90));
h.put("用户 2",new Integer(50));
h.put("用户 3",new Integer(60));
h.put("用户 4",new Integer(70));
h.put("用户 5",new Integer(80));