Why HashTable and Vector are gradually abandoned

Keywords: Programming Java

HashTable, the key value is not allowed to be null. The other is that put method uses synchronized method for thread synchronization, single thread does not need synchronization, and multi thread can use the type of concurren t package. As mentioned in the programming thought, as a tool class, none of the method variables that are not well closed are final. As a container in the era of Java 1.0 / 1.1, it is gradually replaced by HashMap. Vector also has the same problem. As a common set class, the method of adding data is synchronized, and the efficiency is greatly affected.

HashTable Source code put,Key and value are not allowed to be null 
   /**
     * 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     Object#equals(Object)
     * @see     #get(Object)
     */
    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();//key is null, hashcode cannot be calculated and will also report null pointer
        int index = (hash & 0x7FFFFFFF) % tab.length;
        @SuppressWarnings("unchecked")
        Entry<K,V> entry = (Entry<K,V>)tab[index];
        for(; entry != null ; entry = entry.next) {
            if ((entry.hash == hash) && entry.key.equals(key)) {
                V old = entry.value;
                entry.value = value;
                return old;
            }
        }

        addEntry(hash, key, value, index);
        return null;
    }

Posted by izlik on Sat, 30 Nov 2019 09:02:26 -0800