Ejemplo n.º 1
0
  /**
   * Maps the specified <code>key</code> to the specified <code>value</code> in this hashtable.
   * Neither the key nor the value can be <code>null</code>.
   *
   * <p>The value can be retrieved by calling the <code>get</code> method with a key that is equal
   * to the original key.
   *
   * @param key the hashtable key.
   * @param value the value.
   * @return the previous value of the specified key in this hashtable, or <code>null</code> if it
   *     did not have one.
   * @exception NullPointerException if the key or value is <code>null</code>.
   * @see java.lang.Object#equals(java.lang.Object)
   * @see java.util.Hashtable#get(java.lang.Object)
   * @since JDK1.0
   */
  public Object put(Object key, Object value) {
    // Make sure the value is not null
    if (value == null) {
      throw new NullPointerException();
    }

    // Makes sure the key is not already in the hashtable.
    HashtableEntry tab[] = table;
    int hash = key.hashCode();
    int index = (hash & 0x7FFFFFFF) % tab.length;
    for (HashtableEntry e = tab[index]; e != null; e = e.next) {
      if ((e.hash == hash) && e.key.equals(key)) {
        Object old = e.value;
        e.value = value;
        return old;
      }
    }

    if (count >= threshold) {
      // Rehash the table if the threshold is exceeded
      rehash();
      return put(key, value);
    }

    // Creates the new entry.
    HashtableEntry e = new HashtableEntry();
    e.hash = hash;
    e.key = key;
    e.value = value;
    e.next = tab[index];
    tab[index] = e;
    count++;
    return null;
  }
Ejemplo n.º 2
0
  /**
   * Associate the specified value with the specified key in this {@code 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 key, or {@code null} if the key did not
   *     exist.
   * @see #elements
   * @see #get
   * @see #keys
   * @see java.lang.Object#equals
   */
  public synchronized V put(K key, V value) {
    if (key == null) {
      throw new NullPointerException("key == null");
    } else if (value == null) {
      throw new NullPointerException("value == null");
    }
    int hash = secondaryHash(key.hashCode());
    HashtableEntry<K, V>[] tab = table;
    int index = hash & (tab.length - 1);
    HashtableEntry<K, V> first = tab[index];
    for (HashtableEntry<K, V> e = first; e != null; e = e.next) {
      if (e.hash == hash && key.equals(e.key)) {
        V oldValue = e.value;
        e.value = value;
        return oldValue;
      }
    }

    // No entry for key is present; create one
    modCount++;
    if (size++ > threshold) {
      rehash(); // Does nothing!!
      tab = doubleCapacity();
      index = hash & (tab.length - 1);
      first = tab[index];
    }
    tab[index] = new HashtableEntry<K, V>(key, value, hash, first);
    return null;
  }
Ejemplo n.º 3
0
 protected Object clone() {
   HashtableEntry entry = new HashtableEntry();
   entry.hash = hash;
   entry.key = key;
   entry.value = value;
   entry.next = (next != null) ? (HashtableEntry) next.clone() : null;
   return entry;
 }
Ejemplo n.º 4
0
  /**
   * Puts the key and the value in the table. If there already is a key equal ignore case to the one
   * passed the new value exchhanes the old one.
   *
   * @param key String key
   * @param value object to put
   * @return old value if any, or null if none
   * @exception IllegalArgumentException if key is not a string
   */
  public synchronized Object put(Object key, Object value) throws IllegalArgumentException {

    if (value == null) {
      throw new NullPointerException();
    }
    if (table == null) initTable(MIN_CAPACITY);
    try {
      // Makes sure the key is not already in the hashtable.
      int hash = hashCode((String) key);
      int index;
      HashtableEntry[] tab = null;
      do {
        tab = table;
        index = (hash & 0x7FFFFFFF) % tab.length;
        for (HashtableEntry e = tab[index]; e != null; e = e.next) {
          if ((e.hash == hash) && e.key.equalsIgnoreCase((String) key)) {
            Object old = e.value;
            e.value = value;
            return old;
          }
        }
        if (count >= threshold) {
          // Rehash the table if the threshold is exceeded
          rehash();
          continue;
        }
        break;
      } while (true);

      // Creates the new entry.
      HashtableEntry e = new HashtableEntry();
      e.hash = hash;
      e.key = (String) key;
      e.value = value;
      e.next = tab[index];
      tab[index] = e;
      count++;
      return null;
    } catch (ClassCastException cce) {
      throw new IllegalArgumentException("Non string keys are not accepted!");
    }
  }
Ejemplo n.º 5
0
  /**
   * This method is just like put, except that it doesn't do things that are inappropriate or
   * unnecessary for constructors and pseudo-constructors (i.e., clone, readObject). In particular,
   * this method does not check to ensure that capacity is sufficient, and does not increment
   * modCount.
   */
  private void constructorPut(K key, V value) {
    if (key == null) {
      throw new NullPointerException("key == null");
    } else if (value == null) {
      throw new NullPointerException("value == null");
    }
    int hash = secondaryHash(key.hashCode());
    HashtableEntry<K, V>[] tab = table;
    int index = hash & (tab.length - 1);
    HashtableEntry<K, V> first = tab[index];
    for (HashtableEntry<K, V> e = first; e != null; e = e.next) {
      if (e.hash == hash && key.equals(e.key)) {
        e.value = value;
        return;
      }
    }

    // No entry for key is present; create one
    tab[index] = new HashtableEntry<K, V>(key, value, hash, first);
    size++;
  }