Differences between HashMap and HashTable in Java Last Updated : 23 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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 within the table. Now let us discuss with the help of an example. Hashmap vs Hashtable HashMap is non-synchronized. It is not thread-safe and can't be shared between many threads without proper synchronization code whereas Hashtable is synchronized. It is thread-safe and can be shared with many threads.HashMap allows one null key and multiple null values whereas Hashtable doesn't allow any null key or value.HashMap is generally preferred over HashTable if thread synchronization is not needed.Difference Between Hashmap and HashtableS. No.HashmapHashtable1.No method is synchronized.Every method is synchronized.2.Multiple threads can operate simultaneously and hence hashmap's object is not thread-safe.At a time only one thread is allowed to operate the Hashtable's object. Hence it is thread-safe.3.Threads are not required to wait and hence relatively performance is high.It increases the waiting time of the thread and hence performance is low.4.Null is allowed for both key and value.Null is not allowed for both key and value. Otherwise, we will get a null pointer exception.5.It is introduced in the 1.2 version.It is introduced in the 1.0 version.6. It is non-legacy.It is a legacy. Now you must be wondering why HashTable doesn't allow null and HashMap do? The answer is simple. In order to successfully store and retrieve objects from a HashTable, the objects used as keys must implement the hashCode method and the equals method. Since null is not an object, it can't implement these methods. HashMap is an advanced version and improvement on the Hashtable. HashMap was created later. Example: Java // Java program to demonstrate // HashMap and HashTable import java.util.*; import java.lang.*; import java.io.*; // Name of the class has to be "Main" // only if the class is public class Ideone { public static void main(String args[]) { //----------hashtable ------------------------- Hashtable<Integer,String> ht=new Hashtable<Integer,String>(); ht.put(101," ajay"); ht.put(101,"Vijay"); ht.put(102,"Ravi"); ht.put(103,"Rahul"); System.out.println("-------------Hash table--------------"); for (Map.Entry m:ht.entrySet()) { System.out.println(m.getKey()+" "+m.getValue()); } //----------------hashmap-------------------------------- HashMap<Integer,String> hm=new HashMap<Integer,String>(); hm.put(100,"Amit"); hm.put(104,"Amit"); hm.put(101,"Vijay"); hm.put(102,"Rahul"); System.out.println("-----------Hash map-----------"); for (Map.Entry m:hm.entrySet()) { System.out.println(m.getKey()+" "+m.getValue()); } } } Output-------------Hash table-------------- 103 Rahul 102 Ravi 101 Vijay -----------Hash map----------- 100 Amit 101 Vijay 102 Rahul 104 Amit This article is compiled by Aditya Goel. Comment More infoAdvertise with us Next Article Differences between HashMap and HashTable in Java kartik Follow Improve Article Tags : Java Java-HashMap Java-HashTable Java-Map-Programs Practice Tags : Java Similar Reads Hashtable in Java Hashtable class, introduced as part of the Java Collections framework, implements a hash table that 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 an 12 min read HashTable forEach() method in Java with Examples The forEach(BiConsumer) method of Hashtable class perform the BiConsumer operation on each entry of hashtable until all entries have been processed or the action throws an exception. The BiConsumer operation is a function operation of key-value pair of hashtable performed in the order of iteration. 2 min read Hashtable computeIfAbsent() method in Java with Examples The computeIfAbsent(Key, Function) method of Hashtable class which allows you to compute value of a mapping for specified key if key is not already associated with a value (or is mapped to null). If mapping function of this method returns null, then no mapping is recorded. If the remapping function 2 min read HashTable putIfAbsent() method in Java with Examples The putIfAbsent(Key, value) method of Hashtable class which allows to map a value to a given key if given key is not associated with a value or mapped to null. A null value is returned if such key-value set is already present in the HashMap. Syntax: public V putIfAbsent(K key, V value) Parameters: T 2 min read Java Hashtable put() Method The Hashtable.put() method is a part of java.util package. This method is used to insert a mapping into a table. This means we can insert a specific key and the value it maps to into a particular table. If an existing key is passed, then the previous value gets replaced by the new value. If a new pa 2 min read Java Hashtable get() Method The Hashtable.get() method is a built-in method of the java.util.Hashtable class. This method is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the table contains no such mapping for the key. In this article, we will learn about the Ha 3 min read Java Hashtable isEmpty() Method The Hashtable.isEmpty() method is a built-in method of the java.util.Hashtable class. This method is used to check if the table is empty or not. The method returns true, if no key-value pair or mapping is present in the table, else it returns false.Syntax of Hashtable isEmpty() Methodhash_table.isEm 2 min read Hashtable size() Method in Java The java.util.Hashtable.size() method of Hashtable class is used to get the size of the table which refers to the number of the key-value pair or mappings in the Table. Syntax: Hash_Table.size() Parameters: The method does not take any parameters. Return Value: The method returns the size of the tab 2 min read Hashtable remove() Method in Java The java.util.Hashtable.remove() is an inbuilt method of Hashtable class and is used to remove the mapping of any particular key from the table. It basically removes the values for any particular key in the Table.Syntax: Hash_Table.remove(Object key) Parameters: The method takes one parameter key wh 2 min read Hashtable keys() Method in Java As we all know enumeration defines java class type so do enumerations can have constructors, methods, and instance variables. The java.util.Hashtable.keys() method of Hashtable class in Java is used to get the enumeration of the keys present in the hashtable. Illustration: Syntax: public Enumeration 2 min read Like