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;
}
Java에 HashTable 클래스에 put 메소드다. 이중에 hashCode 메소드를 통해 얻은 hash 값을 16진수로 비트 연산을 하는 것에 대한 글이다.
이것은 배열의 인덱스를 참조할 때 음수의 값은 허용되지 않기 때문이다.