hashtable可以有重复的键和值吗?作者: 李尧 时间: 2013-3-28 01:43
不能包含重复的键,每个键最多只能映射到一个值。作者: 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();