此时Hashtable中只有一个键值对,zhangsan这个键对应的值为26,但是put("zhangsan","26")会返回被覆盖的值,即24作者: fighting 时间: 2013-3-28 11:32
呵呵,以前还没有注意。看了看hashtable的代码,才发现这里有不少东西啊
这是hashtable中的put方法,与大家分享一下:
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;
for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
V old = e.value;
e.value = value;
return old;
}
}
modCount++;
if (count >= threshold) {
// Rehash the table if the threshold is exceeded
rehash();