1. 程式人生 > >hashtable,concurrenthashmap為什麼鍵和值不能為null,而hashmap可以?

hashtable,concurrenthashmap為什麼鍵和值不能為null,而hashmap可以?

因為hashtable,concurrenthashmap它們是用於多執行緒的,併發的 ,如果map.get(key)得到了null,不能判斷到底是對映的value是null,還是因為沒有找到對應的key而為空,而用於單執行緒狀態的hashmap卻可以用containKey(key) 去判斷到底是否包含了這個null。

hashtable為什麼就不能containKey(key)
一個執行緒先get(key)再containKey(key),這兩個方法的中間時刻,其他執行緒怎麼操作這個key都會可能發生,例如刪掉這個key