Home > Java > javaTutorial > body text

Analysis of the difference between Hashtable and HashMap in java

巴扎黑
Release: 2017-05-21 14:22:24
Original
1479 people have browsed it

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:
Copy after login

The code is as follows:

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

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)
Copy after login

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());
   }
Copy after login

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;
Copy after login

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;
     }
Copy after login

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);
     }
Copy after login

The code is as follows:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!