问题
Are there are differences between hashmap and hashtable in theory?
I don't mean in the concrete definitions given in Java (or the implementation), but in theory. Isn't a hashtable a map that uses hashing ... hence a hashmap?
回答1:
According to Wikipedia, they are the same:
In computing, a hash table (hash map) is a data structure used to implement an associative array (...)
According to Wikibooks, it's the same:
A hash table, or a hash map, is a data structure that associates keys with values.
Some answer on StackOverflow also states:
Hashtable is often useful (they are also called hashmaps) (...)
回答2:
Hash Map & Hashtable
- Both belongs to Map Interface. Keys – cannot be duplicate.
- Hash Map not synchronized but we can convert it to synchronizedMap. It allows maximum one null key and any number of null values.
- Hash Table synchronized as it is legacy class. It not allow null either in key/value.
The default initial capacity of Hashtable is 11, HashMap is 16.
Hash – will not ensure the order of insertion
- For More information see my stack post
public static void main(String[] args) {
new Thread() {
@Override public void run() {
HashMap<String, Integer> hm = new HashMap<String, Integer>();
hm.put("key0", 10); // Compiler Widens.
hm.put("key1", null);
hm.put("key0", new Integer(16)); // Overridden.
hm.put(null, 20);
hm.put(null, 70);
hm.put(null, null);
System.out.println("HashMap : "+hm); // hm.toString()
Iterator<Entry<String, Integer>> it = hm.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Integer> pair = (Map.Entry<String, Integer>)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
it.remove(); // avoids a ConcurrentModificationException
}
// we can conver HashMap to synchronizedMap
Collections.synchronizedMap(hm);
}
}.start();
new Thread() {
@Override public void run() {
Hashtable<String, Integer> ht = new Hashtable<String, Integer>();
try {
ht.put("product1", 12);
ht.put("product2", 13);
ht.put("product2", 14);
// ht.put(null, 20);
// ht.put("product2", null);
System.out.println("hash Table : "+ht);
} catch (NullPointerException nul) {
System.out.println("HashTable will not accept null's eighter in key/value");
IllegalArgumentException iae = new IllegalArgumentException("nulls not accept");
iae.initCause(nul);
throw iae;
}
}
}.start();
}
回答3:
Synchronization or Thread Safe :This is the most important difference between two . HashMap is non synchronized and not thread safe.On the other hand, HashTable is thread safe and synchronized.HashMap should be used if your application do not require any multi-threading task, in other words hashmap is better for non-threading applications. HashTable should be used in multithreading applications.
Null keys and null values :Hashmap allows one null key and any number of null values, while Hashtable do not allow null keys and null values in the HashTable object.
Performance :Hashmap is much faster and uses less memory than Hashtable as former is unsynchronized . Unsynchronized objects are often much better in performance in compare to synchronized object like Hashtable in single threaded environment.
HashMap inherits AbstractMap class while Hashtable inherits Dictionary class.
The significant difference between HashMap vs Hashtable is that Iterator in the HashMap is a fail-fast iterator while the enumerator for the Hashtable is not and throw ConcurrentModificationException if any other Thread modifies the map structurally by adding or removing any element except Iterator’s own remove() method. But this is not a guaranteed behavior and will be done by JVM on best effort. This is also an important difference between Enumeration and Iterator in Java.
来源:https://stackoverflow.com/questions/30110252/what-are-the-differences-between-hashmap-vs-hashtable-in-theory