Example #1
0
 public V remove(int key) {
   int h = hash(key);
   Entry<V>[] t = table;
   int index = h & (t.length - 1);
   for (Entry<V> e = t[index], pre = null; e != null; pre = e, e = e.next) {
     if (e.key == key) {
       if (pre == null) t[index] = e.next;
       else pre.next = e.next;
       --size;
       return e.value;
     }
   }
   return null;
 }
Example #2
0
  @SuppressWarnings("unchecked")
  private void doubleCapacity() {
    if (table.length == MAXIMUM_CAPACITY) return;

    int newCapacity = table.length * 2;
    Entry<V>[] newTable = (Entry<V>[]) new Entry[newCapacity];
    threshold = (newCapacity >> 1) + (newCapacity >> 2); // 3/4 capacity
    if (size == 0) {
      table = newTable;
      return;
    }

    for (int i = 0, len = table.length; i < len; ++i) {
      Entry<V> e = table[i];
      while (e != null) {
        Entry<V> next = e.next;
        int index = hash(e.key) & (newTable.length - 1);
        e.next = newTable[index];
        newTable[index] = e;
        e = next;
      }
    }
    table = newTable;
  }