2016-10-25 2 views
0

알다시피, 은 Hashtable에 허용되지 않습니다. 그러나 Hashtable (jdk 1.8)의 소스 코드를 확인했습니다. 나는 값의 수표 만보고 열쇠 수표를 찾을 수 없었다. 다음은 소스 코드 put 방법의 아래 :해시 테이블에서 키가 null인지 확인하는 방법 및시기

public synchronized V put(K key, V value) { 
    // Make sure the value is not null 
    if (value == null) { 
     throw new NullPointerException(); 
    } 

    // Makes sure the key is not already in the hashtable. 
    Entry<?,?> tab[] = table; 
    int hash = key.hashCode(); 
    int index = (hash & 0x7FFFFFFF) % tab.length; 
    @SuppressWarnings("unchecked") 
    Entry<K,V> entry = (Entry<K,V>)tab[index]; 
    for(; entry != null ; entry = entry.next) { 
     if ((entry.hash == hash) && entry.key.equals(key)) { 
      V old = entry.value; 
      entry.value = value; 
      return old; 
     } 
    } 

    addEntry(hash, key, value, index); 
    return null; 
} 

답변

5

중요한 검사는 여기에 있습니다 : 키가 null의 경우는 NullPointerException 발생합니다

int hash = key.hashCode(); 

.

관련 문제