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. I
Hash Map & Hashtable
The default initial capacity of Hashtable is 11, HashMap is 16.
Hash – will not ensure the order of insertion
public static void main(String[] args) {
new Thread() {
@Override public void run() {
HashMap hm = new HashMap();
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> it = hm.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)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 ht = new Hashtable();
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();
}