java hashmap - What are the differences between a HashMap and a Hashtable in Java?

java hashmap containskey / java / collections / hashmap / hashtable

What are the differences between a HashMap and a Hashtable in Java?

Steve Chambers



Answer #1

For example, compare Java 5 Map iterating:

for (Elem elem : map.keys()) {
  elem.doSth();
}

versus the old Hashtable approach:

for (Enumeration en = htable.keys(); en.hasMoreElements(); ) {
  Elem elem = (Elem) en.nextElement();
  elem.doSth();
}

In Java 1.8 we are also promised to be able to construct and access HashMaps like in good old scripting languages:

Map<String,Integer> map = { "orange" : 12, "apples" : 15 };
map["apples"];