Difference Between Hashtable and Synchronized Map in Java Last Updated : 22 Feb, 2021 Comments Improve Suggest changes Like Article Like Report The Hashtable class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a Hashtable, the objects used as keys must implement the hashCode method and also the equals method. Features of Hashtable: It is kind of like HashMap but is synchronized.Hashtable stores key/value pair in the hash table.In Hashtable we specify an object that is used as a key, and the value we want to associate with that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table.The initial default capacity of Hashtable class is 11 whereas load Factor is 0.75.HashMap doesn’t provide any Enumeration, while Hashtable provides not fail-fast Enumeration. Example: Java // Java program to demonstrate the // usage of HashTable import java.util.*; class Hashtable1 { public static void main(String args[]) { // Creating a HashTable Hashtable<Integer, String> mytable = new Hashtable<Integer, String>(); // Adding elements to HashTable mytable.put(1, "James Bond"); mytable.put(2, "Donald Trumph"); mytable.put(3, "Joe Biden"); mytable.put(4, "Mona Lisa"); // Iterating through HashTable for (Map.Entry m : mytable.entrySet()) { System.out.println(m.getKey() + " " + m.getValue()); } } } Output4 Mona Lisa 3 Joe Biden 2 Donald Trumph 1 James Bond SynchronizedHashMap Synchronization at the Object level.Every read/write operation needs to acquire lock.Locking the whole collection is a performance overhead.This essentially gives access to just one thread to the entire map & blocks all the other threads.It may cause contention.SynchronizedHashMap returns Iterator, which fails-fast on concurrent modification. Example: Java // Java program to demonstrate the // usage of Synchronized HashMap import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class HashMapSyncExample { public static void main(String args[]) { // Creating HashMap HashMap<Integer, String> hmap = new HashMap<Integer, String>(); // Adding elements in HashMap hmap.put(2, "Anil"); hmap.put(44, "Ajit"); hmap.put(1, "Brad"); hmap.put(4, "Sachin"); hmap.put(88, "XYZ"); Map map = Collections.synchronizedMap(hmap); Set set = map.entrySet(); synchronized (map) { Iterator i = set.iterator(); // Display elements while (i.hasNext()) { Map.Entry me = (Map.Entry)i.next(); System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } } } } Output1: Brad 2: Anil 4: Sachin 88: XYZ 44: AjitHashtable vs SynchronizedHashMap Hashtable Synchronized HashMap Hashtable doesn’t allow even a single null key and null values. Synchronized HashMap allows one null key and any number of null values. Iterators returned by Hashtable are fail-safe in nature. i.e they don’t throw ConcurrentModificationException if the map is modified after the creation of the iterator. Iterators returned by synchronized HashMap are fail-fast in nature. i.e they throw ConcurrentModificationException if the map is modified after the creation of iterator. HashTable was there since JDK 1.1. From JDK 1.2, it has been made a part of Java Collection Framework. HashMap is introduced in JDK 1.2. HashTable is the legacy class. It is sometimes considered as due for deprecation. So, it is recommended that not to use HashTable in your applications. If you want a high level of data consistency, then only consider using synchronized HashMap. Comment More infoAdvertise with us Next Article Difference Between Hashtable and Synchronized Map in Java K kashish2008 Follow Improve Article Tags : Java Technical Scripter Difference Between Technical Scripter 2020 Java-Collections java-map Java-HashTable +3 More Practice Tags : JavaJava-Collections Similar Reads Differences between HashMap and HashTable in Java HashMap and Hashtable store key and value pairs in a hash table. When using a Hashtable or HashMap, we specify an object that is used as a key and the value that you want to be linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored w 3 min read Difference between List, Set and Map in Java List interface in Java is a sub-interface of the Java collections interface. It contains the index-based methods to insert, update, delete, and search the elements. It can have duplicate elements also. We can also store the null elements in the list. List preserves the insertion order, it allows pos 4 min read Differences between TreeMap, HashMap and LinkedHashMap in Java Prerequisite: HashMap and TreeMap in Java TreeMap, HashMap and LinkedHashMap: What's Similar? All offer a key->value map and a way to iterate through the keys. The most important distinction between these classes is the time guarantees and the ordering of the keys.All three classes HashMap, TreeM 5 min read Difference between HashMap and HashSet HashSet is an implementation of Set Interface which does not allow duplicate value. The main thing is, objects that are stored in HashSet must override equals() for check for equality, and hashCode() methods for no duplicate value are stored in our set. HashMap is an implementation of Map Interface, 4 min read Difference Between keySet() and entrySet() Method in Java Map Map Interface is present in Java.util package, which provides mainly three methods KeySet(),entrySet() and values(). These methods are used to retrieve the keys of the map, key-value pairs of the map, and values of the map respectively. Since these methods are part of Map Interface, so we can use th 4 min read Like