av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

java語言

Java中hashmap和hashtable的區(qū)別

時(shí)間:2025-04-15 18:39:03 java語言 我要投稿
  • 相關(guān)推薦

Java中hashmap和hashtable的區(qū)別

  引導(dǎo)語:HashMap 和HashSet 是Java Collection Framework 的兩個(gè)重要成員,其中 HashMap 是Map 接口的常用實(shí)現(xiàn)類,HashSet 是Set 接口的常用實(shí)現(xiàn)類。以下是百分網(wǎng)小編分享給大家的Java中hashmap和hashtable的區(qū)別,歡迎閱讀!

  1、 繼承和實(shí)現(xiàn)區(qū)別

  Hashtable是基于陳舊的Dictionary類,完成了Map接口;HashMap是Java 1.2引進(jìn)的Map接口的一個(gè)實(shí)現(xiàn)(HashMap繼承于AbstractMap,AbstractMap完成了Map接口)。

  2、 線程安全不同

  HashTable的方法是同步的,HashMap是未同步,所以在多線程場(chǎng)合要手動(dòng)同步HashMap。

  3、 對(duì)null的處理不同

  HashTable不允許null值(key和value都不可以),HashMap允許null值(key和value都可以)。即 HashTable不允許null值其實(shí)在編譯期不會(huì)有任何的不一樣,會(huì)照樣執(zhí)行,只是在運(yùn)行期的時(shí)候Hashtable中設(shè)置的話回出現(xiàn)空指針異常。 HashMap允許null值是指可以有一個(gè)或多個(gè)鍵所對(duì)應(yīng)的值為null。當(dāng)get()方法返回null值時(shí),即可以表示 HashMap中沒有該鍵,也可以表示該鍵所對(duì)應(yīng)的值為null。因此,在HashMap中不能由get()方法來判斷HashMap中是否存在某個(gè)鍵,而應(yīng)該用containsKey()方法來判斷。

  4、 方法不同

  HashTable有一個(gè)contains(Object value),功能和containsValue(Object value)功能一樣。

  5、HashTable使用Enumeration,HashMap使用Iterator。

  6、HashTable中hash數(shù)組默認(rèn)大小是11,增加的方式是 old*2+1。HashMap中hash數(shù)組的默認(rèn)大小是16,而且一定是2的指數(shù)。

  7、哈希值的使用不同,HashTable直接使用對(duì)象的hashCode,代碼是這樣的:

  int hash = key.hashCode();

  int index = (hash & 0x7FFFFFFF) % tab.length;

  而HashMap重新計(jì)算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);

  }

區(qū)別

Hashtable

Hashmap

繼承、實(shí)現(xiàn)

Hashtable extends Dictionaryimplements Map, Cloneable,Serializable

HashMap extends AbstractMap implements Map, Cloneable,Serializable

線程同步

已經(jīng)同步過的可以安全使用

未同步的,可以使用Colletcions進(jìn)行同步Map Collections.synchronizedMap(Map m)

對(duì)null的處理

Hashtable table = new Hashtable();

table.put(null, "Null");

table.put("Null", null);

table.contains(null);

table.containsKey(null);

table.containsValue(null);

后面的5句話在編譯的時(shí)候不會(huì)有異常,可在運(yùn)行的時(shí)候會(huì)報(bào)空指針異常具體原因可以查看源代碼

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條語句無論在編譯期,還是在運(yùn)行期都是沒有錯(cuò)誤的.

在HashMap中,null可以作為鍵,這樣的鍵只有一個(gè);可以有一個(gè)或多個(gè)鍵所對(duì)應(yīng)的值為null。當(dāng)get()方法返回null值時(shí),即可以表示 HashMap中沒有該鍵,也可以表示該鍵所對(duì)應(yīng)的值為null。因此,在HashMap中不能由get()方法來判斷HashMap中是否存在某個(gè)鍵,而應(yīng)該用containsKey()方法來判斷。

增長率

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

Entry 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 e = table[bucketIndex];

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

if (size++ >= threshold)

resize(2 * table.length);

}

哈希值的使用

HashTable直接使用對(duì)象的hashCode,代碼是這樣的:

public synchronized booleancontainsKey(Object key) {

Entry tab[] = table;

int hash = key.hashCode();

int index = (hash & 0x7FFFFFFF) % tab.length;

for (Entry e = tab[index] ; e !=null ; e = e.next) {

if ((e.hash == hash) && e.key.equals(key)) {

return true;

}

}

return false;

}

HashMap重新計(jì)算hash值,而且用與代替求模

public boolean containsKey(Object key) {

Object k = maskNull(key);

int hash = hash(k.hashCode());

int i = indexFor(hash, table.length);

Entry e = table[i];

while (e != null) {

if (e.hash == hash && eq(k, e.key))

return true;

e = e.next;

}

return false;

}


【Java中hashmap和hashtable的區(qū)別】相關(guān)文章:

對(duì)Java中HashMap和TreeMap的區(qū)別的深入理解06-09

Java中的堆和棧的區(qū)別05-23

java中String和StringBuffer的區(qū)別08-01

java中l(wèi)ength和length()的區(qū)別04-18

關(guān)于java中堆和棧的區(qū)別01-13

java中i++和++i的區(qū)別03-13

Java中對(duì)象和引用的具體區(qū)別05-18

Java中靜態(tài)綁定和動(dòng)態(tài)綁定的區(qū)別08-14

深入解析hashmap,java實(shí)現(xiàn)原理08-08