1.

Difference Between Hashmap And Hashtable In Java?

Answer»

Though both HashTable and HashMap store elements as a (key, value) pair and use hashing technique to store elements, moreover from Java v1.2, HashTable class was retrofitted to implement the MAP interface, making it a member of the Java COLLECTIONS Framework. But there are certain difference between the TWO -

  • HashMap is not synchronized where as HashTable is synchronized.
  • HashMap allows one null value as a key and any NUMBER of null VALUES where as HashTable does not allow null values either as key or as value.
  • For traversing a HashMap an iterator can be used. For traversing a HashTable either an iterator or Enumerator can be used. The iterator used for both HashMap and HashTable is fail-fast but the enumerator used with HashTable is fail-safe.
  • Performance wise HashMap is faster than the HashTable reason being HashMap is not synchronized.

Though both HashTable and HashMap store elements as a (key, value) pair and use hashing technique to store elements, moreover from Java v1.2, HashTable class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework. But there are certain difference between the two -



Discussion

No Comment Found