网站首页 语言 会计 电脑 医学 资格证 职场 文艺体育 范文
当前位置:书香门第 > 计算机 > java语言

Java中hashmap和hashtable的区别

栏目: java语言 / 发布于: / 人气:6.47K

引导语:HashMap 和HashSet 是Java Collection Framework 的两个重要成员,其中 HashMap 是Map 接口的常用实现类,HashSet 是Set 接口的常用实现类。以下是本站小编分享给大家的Java中hashmap和hashtable的区别,欢迎阅读!

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,代码是这样的:

int hash = Code();

int index = (hash & 0x7FFFFFFF) % th;

而HashMap重新计算hash值,而且用与代替求模:

int hash = hash(k);

int i = indexFor(hash, th);

static int hash(Object x) {

int h = Code();

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

继承、实现

Hashtableextends Dictionaryimplements Map, Cloneable,Serializable

HashMapextends AbstractMapimplements Map, Cloneable,Serializable

线程同步

已经同步过的可以安全使用

未同步的,可以使用Colletcions进行同步MaphronizedMap(Mapm)

对null的处理

Hashtable table = new Hashtable();

(null, "Null");

("Null", null);

ains(null);

ainsKey(null);

ainsValue(null);

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

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();
(null, "Null");

("Null", null);

ainsKey(null);

ainsValue(null);

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

在HashMap中,null可以作为键,这样的键只有一个;可以有一个或多个键所对应的值为null。当get()方法返回null值时,即可以表示 HashMap中没有该键,也可以表示该键所对应的值为null。因此,在HashMap中不能由get()方法来判断HashMap中是否存在某个键,而应该用containsKey()方法来判断。

增长率

protected void rehash() {

int oldCapacity = th;

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 (Entryold = oldMap[i] ; old != null ; ) {

Entrye = old;

old = ;

int index = ( & 0x7FFFFFFF) % newCapacity;

= newMap[index];

newMap[index] = e;

}

}

}

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

Entrye = table[bucketIndex];

table[bucketIndex] = new Entry(hash, key, value, e);

if (size++ >= threshold)

resize(2 * th);

}

哈希值的使用

HashTable直接使用对象的hashCode,代码是这样的:

publicsynchronizedbooleancontainsKey(Object key) {

Entry tab[] = table;

inthash = Code();

intindex = (hash & 0x7FFFFFFF) % th;

for(Entrye = tab[index] ; e !=null; e = ) {

if(( == hash) && ls(key)) {

returntrue;

}

}

returnfalse;

}

HashMap重新计算hash值,而且用与代替求模

public boolean containsKey(Object key) {

Object k = maskNull(key);

int hash = hash(Code());

int i = indexFor(hash, th);

Entry e = table[i];

while (e != null) {

if ( == hash && eq(k, ))

return true;

e = ;

}

return false;

}