Home >Java >javaTutorial >Analysis of the difference between Hashtable and HashMap in java

Analysis of the difference between Hashtable and HashMap in java

巴扎黑
巴扎黑Original
2017-05-21 14:22:241494browse

Analysis of the difference between Hashtable and HashMap in java, friends in need can refer to it

1. Hashtable is a subclass of Dictionary,

The code is as follows:

 public class Hashtable<K,V>
     extends Dictionary<K,V>
     implements Map<K,V>, Cloneable, java.io.Serializable 
HashMap:

The code is as follows:

public class HashMap<K,V>
    extends AbstractMap<K,V>
     implements Map<K,V>, Cloneable, Serializable

HashMap and Hashtable are both implementation classes of the Map interface;

2. The methods in Hashtable are synchronous (), while the methods in HashMap are by default It's not synchronized. That is to say, in multi-threaded applications, Hashtable can be used safely without special operations; for HashMap, additional synchronization mechanism is required. But the synchronization problem of HashMap can be solved through a static method of Collections:

The code is as follows:

public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m)

This method returns a synchronized Map, which means that the returned Map is a thread safe. It should be noted that when iterating the returned map, you must manually synchronize the returned map, otherwise it will lead to uncertain behavior:

The code is as follows:

Map m = Collections.synchronizedMap(new HashMap());
       ...
   Set s = m.keySet();  // Needn&#39;t be in synchronized block
       ...
   synchronized(m) {  // Synchronizing on m, not s!
       Iterator i = s.iterator(); // Must be in synchronized block
       while (i.hasNext())
           foo(i.next());
   }

3. In HashMap, null can be used as a key, and there is only one such key; there can be one or more keys whose corresponding value is null. When the get() method returns a null value, it can mean that the key does not exist in the HashMap, or it can also mean that the value corresponding to the key is null. Therefore, in HashMap, the get() method cannot be used to determine whether a certain key exists in the HashMap, but the containsKey() method should be used to determine. The key value of Hashtable cannot be null, otherwise: java.lang.NullPointerException.
4.HashTable uses Enumeration, and HashMap uses Iterator.
The above are only superficial differences, and their implementations are also very different.
5. The default size of the hash array in HashTable is 11, and the increasing method is old*2+1. The default size of the hash array in HashMap is 16, and it must be an exponent of 2.
6. The use of hash values ​​is different. HashTable directly uses the hashCode of the object. The code is as follows:

The code is as follows:

int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;

And HashMap recalculates the hash value. And use and instead of modulus, such as the put method of HashMap:

The code is as follows:

public V put(K key, V value) {
         if (key == null)
             return putForNullKey(value);
         int hash = hash(key.hashCode());
         int i = indexFor(hash, table.length);
         for (Entry<K,V> e = table[i]; e != null; e = e.next) {
             Object k;
             if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                 V oldValue = e.value;
                 e.value = value;
                 e.recordAccess(this);
                 return oldValue;
             }
         }
         modCount++;
         addEntry(hash, key, value, i);
         return null;
     }

The code is as follows:

static int hash(int h) {
         // This function ensures that hashCodes that differ only by
         // constant multiples at each bit position have a bounded
         // number of collisions (approximately 8 at default load factor).
         h ^= (h >>> 20) ^ (h >>> 12);
         return h ^ (h >>> 7) ^ (h >>> 4);
     }

The code is as follows:

static int indexFor(int h, int length) {
         return h & (length-1);
     }

The above is the detailed content of Analysis of the difference between Hashtable and HashMap in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn