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

There is many good answer already posted. I'm adding few new points and summarizing it.

HashMap and Hashtable both are used to store data in key and value form. Both are using hashing technique to store unique keys. But there are many differences between HashMap and Hashtable classes that are given below.

HashMap

  1. HashMap is non synchronized. It is not-thread safe and can't be shared between many threads without proper synchronization code.
  2. HashMap allows one null key and multiple null values.
  3. HashMap is a new class introduced in JDK 1.2.
  4. HashMap is fast.
  5. We can make the HashMap as synchronized by calling this code
    Map m = Collections.synchronizedMap(HashMap);
  6. HashMap is traversed by Iterator.
  7. Iterator in HashMap is fail-fast.
  8. HashMap inherits AbstractMap class.

Hashtable

  1. Hashtable is synchronized. It is thread-safe and can be shared with many threads.
  2. Hashtable doesn't allow any null key or value.
  3. Hashtable is a legacy class.
  4. Hashtable is slow.
  5. Hashtable is internally synchronized and can't be unsynchronized.
  6. Hashtable is traversed by Enumerator and Iterator.
  7. Enumerator in Hashtable is not fail-fast.
  8. Hashtable inherits Dictionary class.

Further reading What is difference between HashMap and Hashtable in Java?

enter image description here

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to collections

Kotlin's List missing "add", "remove", Map missing "put", etc? How to unset (remove) a collection element after fetching it? How can I get a List from some class properties with Java 8 Stream? Java 8 stream map to list of keys sorted by values How to convert String into Hashmap in java How can I turn a List of Lists into a List in Java 8? MongoDB Show all contents from all collections Get nth character of a string in Swift programming language Java 8 Distinct by property Is there a typescript List<> and/or Map<> class/library?

Examples related to hashmap

How to split a string in two and store it in a field Printing a java map Map<String, Object> - How? Hashmap with Streams in Java 8 Streams to collect value of Map How to convert String into Hashmap in java Convert object array to hash map, indexed by an attribute value of the Object HashMap - getting First Key value The type java.util.Map$Entry cannot be resolved. It is indirectly referenced from required .class files Sort Go map values by keys Print all key/value pairs in a Java ConcurrentHashMap creating Hashmap from a JSON String

Examples related to hashtable

How do I encode a JavaScript object as JSON? Hash table runtime complexity (insert, search and delete) Looping through a hash, or using an array in PowerShell Hash function for a string hash function for string iterating through Enumeration of hastable keys throws NoSuchElementException error How do HashTables deal with collisions? Good Hash Function for Strings Iterating over and deleting from Hashtable in Java What happens when a duplicate key is put into a HashMap?