求教,java高手请进
我想知道HaspMap的put()方法是如何工作的,主要是将一组新的(key--value)放到合适的位置,以下是jdk的源码
public Object put(Object key, Object value) {
// Makes sure the key is not already in the HashMap.
Entry tab[] = table;
int hash = 0;
int index = 0;
if (key != null) {
hash = key.hashCode();
index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && key.equals(e.key)) {
Object old = e.value;
e.value = value;
return old;
}
}
} else {
for (Entry e = tab[0] ; e != null ; e = e.next) {
if (e.key == null) {
Object old = e.value;
e.value = value;
return old;
}
}
}
modCount++;
if (count >= threshold) {
// Rehash the table if the threshold is exceeded
rehash();
tab = table;
index = (hash & 0x7FFFFFFF) % tab.length;
}
请重点解释一下 index = (hash & 0x7FFFFFFF) % tab.length;