Java中HashMap和HashTable的区别 .

1.继承和实现区别

Hashtable是基于陈旧的Dictionary类,完成了Map接口;HashMap是Java 1.2引进的Map接口的一个实现(HashMap

继承于AbstractMap,AbstractMap完成了Map接口)。

2、 线程安全不同

HashTable的方法是同步的,HashMap是未同步,所以在多线程场合要手动同步HashMap。

3、 对null的处理不同

HashTable不允许null值(key和value都不可以),HashMap允许null值(key和value都可以)。即HashTable不允许

null值其实在编译期不会有任何的不一样,会照样执行,只是在运行期的时候Hashtable中设置的话回出现空指针

异常。HashMap允许null值是指可以有一个或多个键所对应的值为null。当get()方法返回null值时,即可以表示

HashMap中没有该键,也可以表示该键所对应的值为null。因此,在HashMap中不能由get()方法来判断HashMap中

是否存在某个键,而应该用containsKey()方法来判断。

4、 方法不同

HashTable有一个contains(Object value),功能和containsValue(Object value)功能一样。
5、HashTable使用Enumeration,HashMap使用Iterator。

6、HashTable中hash数组默认大小是11,增加的方式是 old*2+1。HashMap中hash数组的默认大小是16,而且一定

是2的指数。
7、哈希值的使用不同,HashTable直接使用对象的hashCode,代码是这样的:

  1. int hash = key.hashCode();  
  2. int index = (hash & 0x7FFFFFFF) % tab.length;  
  3. 而HashMap重新计算hash值,而且用与代替求模:  
  4. int hash = hash(k);  
  5. int i = indexFor(hash, table.length);  
  6. static int hash(Object x) {  
  7.   int h = x.hashCode();  
  8.   h += ~(h << 9);  
  9.   h ^= (h >>> 14);  
  10.   h += (h << 4);  
  11.   h ^= (h >>> 10);  
  12.   return h;  
  13. }  
  14. static int indexFor(int h, int length) {  
  15.   return h & (length-1);  
  16. }  
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);
}

区别
Hashtable
Hashmap

继承、实现

  1. Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable,Serializable  
  2. HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable,Serializable  
Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable,Serializable
HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable,Serializable


线程同步
已经同步过的可以安全使用
未同步的,可以使用Colletcions进行同步Map Collections.synchronizedMap(Map m)

对null的处理

  1. Hashtable table = new Hashtable();  
  2.   
  3. table.put(null"Null");  
  4.   
  5. table.put("Null"null);  
  6.   
  7. table.contains(null);  
  8.   
  9. table.containsKey(null);  
  10.   
  11. table.containsValue(null);  
Hashtable table = new Hashtable();

table.put(null, "Null");

table.put("Null", null);

table.contains(null);

table.containsKey(null);

table.containsValue(null);


后面的5句话在编译的时候不会有异常,可在运行的时候会报空指针异常具体原因可以查看源代码

 

  1. public synchronized V put(K key, V value) {  
  2.   
  3. // Make sure the value is not null   
  4.   
  5. if (value == null) {  
  6.   
  7. throw new NullPointerException();  
  8.   
  9. }  
  10. HashMap map = new HashMap();  
  11.   
  12. map.put(null"Null");  
  13.   
  14. map.put("Null"null);  
  15.   
  16. map.containsKey(null);  
  17.   
  18. map.containsValue(null);  
public synchronized V put(K key, V value) {

// Make sure the value is not null

if (value == null) {

throw new NullPointerException();

}
HashMap map = new HashMap();

map.put(null, "Null");

map.put("Null", null);

map.containsKey(null);

map.containsValue(null);


以上这5条语句无论在编译期,还是在运行期都是没有错误的.

在HashMap中,null可以作为键,这样的键只有一个;可以有一个或多个键所对应的值为null。当get()方法返回

null值时,即可以表示 HashMap中没有该键,也可以表示该键所对应的值为null。因此,在HashMap中不能由get

()方法来判断HashMap中是否存在某个键,而应该用containsKey()方法来判断。

增长率

  1. protected void rehash() {  
  2.   
  3. int oldCapacity = table.length;  
  4.   
  5. Entry[] oldMap = table;  
  6.   
  7. int newCapacity = oldCapacity * 2 + 1;  
  8.   
  9. Entry[] newMap = new Entry[newCapacity];  
  10.   
  11. modCount++;  
  12.   
  13. threshold = (int)(newCapacity * loadFactor);  
  14.   
  15. table = newMap;  
  16.   
  17. for (int i = oldCapacity ; i-- > 0 ;) {  
  18.   
  19. for (Entry<K,V> old = oldMap[i] ; old != null ; ) {  
  20.   
  21. Entry<K,V> e = old;  
  22.   
  23. old = old.next;  
  24.   
  25. int index = (e.hash & 0x7FFFFFFF) % newCapacity;  
  26.   
  27. e.next = newMap[index];  
  28.   
  29. newMap[index] = e;  
  30.   
  31. }  
  32.   
  33. }  
  34.   
  35. }  
  36.   
  37. void addEntry(int hash, K key, V value, int bucketIndex) {  
  38.   
  39. Entry<K,V> e = table[bucketIndex];  
  40.   
  41. table[bucketIndex] = new Entry<K,V>(hash, key, value, e);  
  42.   
  43. if (size++ >= threshold)  
  44.   
  45. resize(2 * table.length);  
  46.   
  47. }   
protected void rehash() {

int oldCapacity = table.length;

Entry[] oldMap = table;

int newCapacity = oldCapacity * 2 + 1;

Entry[] newMap = new Entry[newCapacity];

modCount++;

threshold = (int)(newCapacity * loadFactor);

table = newMap;

for (int i = oldCapacity ; i-- > 0 ;) {

for (Entry<K,V> old = oldMap[i] ; old != null ; ) {

Entry<K,V> e = old;

old = old.next;

int index = (e.hash & 0x7FFFFFFF) % newCapacity;

e.next = newMap[index];

newMap[index] = e;

}

}

}

void addEntry(int hash, K key, V value, int bucketIndex) {

Entry<K,V> e = table[bucketIndex];

table[bucketIndex] = new Entry<K,V>(hash, key, value, e);

if (size++ >= threshold)

resize(2 * table.length);

} 


 

来自: http://wenda.tianya.cn/question/4f23fa201ce5d73d
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值