---
title: "Hashtable和HashMap的区别 "
author: "explore"
date: "2010-04-20T10:14:17Z"
tags: []
categories: ["编程语言"]
slug: "hashtable-hashmap"
draft: false
lastmod: "2010-04-20T10:15:22Z"
id: 648102
url: "https://explore.iteye.com/blog/648102"
---

转自：<http://edu.codepub.com/2009/1118/17775.php>

Hashtable和HashMap的区别 
1.Hashtable是Dictionary的子类，HashMap是Map接口的一个实现类；

2.Hashtable中的方法是同步的，而HashMap中的方法在缺省情况下是非同步的。即是说，在多线程应用程序中，不用专门的操作就安全地可以使用Hashtable了；而对于HashMap，则需要额外的同步机制。但HashMap的同步问题可通过Collections的一个静态方法得到解决：
Map Collections.synchronizedMap(Map m)
这个方法返回一个同步的Map，这个Map封装了底层的HashMap的所有方法，使得底层的HashMap即使是在多线程的环境中也是安全的。

3.在HashMap中，null可以作为键，这样的键只有一个；可以有一个或多个键所对应的值为null。当get()方法返回null值时，即可以表示HashMap中没有该键，也可以表示该键所对应的值为null。因此，在HashMap中不能由get()方法来判断HashMap中是否存在某个键，而应该用containsKey()方法来判断。

4.其底层的实现机制不同，hashmap的访问速度要快于hashtable，因为它不需要进行同步检验，建议在非多线程环境中使用hashmap代替hashtable .

HashMap与Hashtable的区别 
HashTable的应用非常广泛，HashMap是新框架中用来代替HashTable的类，也就是说建议使用HashMap，不要使用HashTable。可能你觉得HashTable很好用，为什么不用呢？这里简单分析他们的区别。 
1.HashTable的方法是同步的，HashMap未经同步，所以在多线程场合要手动同步HashMap这个区别就像Vector和ArrayList一样。

2.HashTable不允许null值(key和value都不可以),HashMap允许null值(key和value都可以)。

3.HashTable有一个contains(Object value)，功能和containsValue(Object value)功能一样。

4.HashTable使用Enumeration，HashMap使用Iterator。

以上只是表面的不同，它们的实现也有很大的不同。

5.HashTable中hash数组默认大小是11，增加的方式是 old*2+1。HashMap中hash数组的默认大小是16，而且一定是2的指数。

6.哈希值的使用不同，HashTable直接使用对象的hashCode，代码是这样的： 
int hash = key.hashCode(); 
int index = (hash & 0x7FFFFFFF) % tab.length; 
而HashMap重新计算hash值，而且用与代替求模： 
int hash = hash(k); 
int i = indexFor(hash, table.length);

static int hash(Object x) { 
　　int h = x.hashCode();

　　h += ~(h < < 9); 
　　h ^= (h >>> 14); 
　　h += (h < < 4); 
　　h ^= (h >>> 10); 
　　return h; 
} 
static int indexFor(int h, int length) { 
　　return h & (length-1); 
} 
以上只是一些比较突出的区别，当然他们的实现上还是有很多不同的，比如 
HashMap对null的操作

例子

package testMapAndTable;

import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

public class TableMap {

public static void main(String... strings) {
Map<Integer, Integer> map = Collections
.synchronizedMap(new HashMap<Integer, Integer>());//为hashmap加上同步
Hashtable<Integer, Integer> table = new Hashtable<Integer, Integer>();
long before = System.currentTimeMillis();
add(map);
System.out.println("add map time="
+ (System.currentTimeMillis() - before));
before = System.currentTimeMillis();
add(table);
System.out.println("add table time="
+ (System.currentTimeMillis() - before));

before = System.currentTimeMillis();
get(map);
System.out.println("get map time="
+ (System.currentTimeMillis() - before));

before = System.currentTimeMillis();
get(table);
System.out.println("get table time="
+ (System.currentTimeMillis() - before));
}

public static void add(Map<Integer, Integer> map) {
for (int i = 0; i < 300000; i++) {
map.put(i, i);
}
}

public static void get(Map<Integer, Integer> map) {
for (int i = 0; i < 300000; i++) {
map.get(i);
}
}
}