Associate the specified value with the specified key in this Hashtable. If the key already exists, the old value is replaced. The key and value cannot be null. @param key the key to add. @param value the value to add. @return the old value associated with the specified
(K key, V value)
| 691 | * @see java.lang.Object#equals |
| 692 | */ |
| 693 | @Override |
| 694 | public synchronized V put(K key, V value) { |
| 695 | if (key != null && value != null) { |
| 696 | int hash = key.hashCode(); |
| 697 | int index = (hash & 0x7FFFFFFF) % elementData.length; |
| 698 | Entry<K, V> entry = elementData[index]; |
| 699 | while (entry != null && !entry.equalsKey(key, hash)) { |
| 700 | entry = entry.next; |
| 701 | } |
| 702 | if (entry == null) { |
| 703 | modCount++; |
| 704 | if (++elementCount > threshold) { |
| 705 | rehash(); |
| 706 | index = (hash & 0x7FFFFFFF) % elementData.length; |
| 707 | } |
| 708 | if (index < firstSlot) { |
| 709 | firstSlot = index; |
| 710 | } |
| 711 | if (index > lastSlot) { |
| 712 | lastSlot = index; |
| 713 | } |
| 714 | entry = newEntry(key, value, hash); |
| 715 | entry.next = elementData[index]; |
| 716 | elementData[index] = entry; |
| 717 | return null; |
| 718 | } |
| 719 | V result = entry.value; |
| 720 | entry.value = value; |
| 721 | return result; |
| 722 | } |
| 723 | throw new NullPointerException(); |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * Copies every mapping to this {@code Hashtable} from the specified map. |