Java Hashtable tutorial with examples will help you understand how to use Java Hashtable in an easy way. Hashtable in Java is an implementation of the Map interface. The Hashtable class in Java does not allow null key or value.
The Hashtable class is contained in the java.util package. You have to import the java.util package using the import statement to use the Hashtable class in the code.
The Hashtable class implements the Map interface and extends the Dictionary class and does not guarantee the order of its elements. That means that the elements may not be returned in the same order in which they were inserted into the hashtable.
In Java version 2, the Hashtable class was rewritten to implement the Map interface and that made it a member of the Java Collection Framework.
Unlike most collection implementations e.g. HashMap, the Hashtable is synchronized. If multi-threaded support is not needed, it is recommended to use the HashMap class instead of the Hashtable class.
How to create new objects of Hashtable?
The Hashtable class provides several constructors using which we can create new objects of the hashtable. The default Hashtable constructor creates a new and empty hashtable object.
1 |
public Hashtable() |
The new object has a default initial capacity of 11 and a default load factor of 0.75.
1 |
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); |
If you want to create a new hashtable object with custom initial capacity and load factor, you can use the below given overloaded constructor.
1 |
public Hashtable(int initialCapacity, float loadFactor) |
The Hashtable class also provides an overloaded constructor that accepts a map object.
1 |
public Hashtable(Map<? extends K,? extends V> map) |
It creates a new Hashtable object containing the same key-value mappings as the specified map object. The new hashtable object will have the initial capacity enough to hold the specified map entries and the default load factor of 0.75.
Java Hashtable Methods
The below given are some of the important methods of the Hashtable class in Java.
How to add mappings to the Hashtable using put, putIfAbsent, and putAll methods?
The Hashtable put
method maps given key with the given value in the hashtable.
1 |
public V put(K key, V value) |
The put
method returns null if the specified key was not mapped to any value previously. If the specified key was mapped to any value, it replaces the old value with new value and returns the old value.
The put
method throws NullPointerException if the key or value is null.
1 2 3 4 5 6 7 8 9 |
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); //this will create mapping 1=>One and return null System.out.println( hashtable.put(1, "One") ); //this will replace "One" with "Two" for key 1 and return "One" System.out.println( hashtable.put(1, "Two") ); System.out.println("Hashtable contains: " + hashtable); |
Output
1 2 3 |
null One Hashtable contains: {1=Two} |
As we can see from the output when the key already exists in the hashtable the put method replaces an old value with the new value for the given key.
If you want to create a new mapping if the key does not exist, but do not want to replace the value if the key already exists then use the putIfAbsent
method.
1 |
public V putIfAbsent(K key, V value) |
The Hashtable putIfAbsent
method maps a given key with the given value and returns null if the key does not exist. If the key exists, it simply returns the value associated with the given key.
1 2 3 4 5 6 7 8 9 |
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); //this will create mapping 1=>One and return null System.out.println( hashtable.putIfAbsent(1, "One") ); //this will return "One" but will not replace the value System.out.println( hashtable.putIfAbsent(1, "Two") ); System.out.println("Hashtable contains: " + hashtable); |
Output
1 2 3 |
null One Hashtable contains: {1=One} |
The Hashtable putAll
method copies all the mappings of the specified map to this hashtable object.
1 |
public void putAll(Map<? extends K,? extends V> map) |
The mappings contained the specified map will replace any values mapped to the matching keys in this hashtable object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); HashMap<Integer, String> hashmap = new HashMap<Integer, String>(); hashmap.put(1, "Ten"); hashmap.put(2, "Two"); hashmap.put(3, "Three"); //this will copy all mappings from hashmap to hashtable hashtable.putAll(hashmap); System.out.println("Hashtable contains: " + hashtable); |
Output
1 |
Hashtable contains: {3=Three, 2=Two, 1=Ten} |
As we can see from the output, the value for key “1” in hashtable was replaced by the value for key “1” in the hashmap object.
How to get value mapped to the given key in the Hashtable using the get and getOrDefault methods?
The Hashtable get
method returns the value associated with the specified key in the hashtable.
1 |
public V get(Object key) |
If the specified key does not exist in the hashtable, it returns null.
1 2 3 4 5 6 7 8 9 10 11 |
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); //this will return "Two" i.e. value for key 2 System.out.println( hashtable.get(2) ); //this will return null as key 4 does not exist System.out.println( hashtable.get(4) ); |
Output
1 2 |
Two null |
If you want to get a specified default value if the key does not exist instead of null, you can use the getOrDefault
method of the Hashtable class.
1 2 3 4 5 6 7 8 9 10 11 |
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); //this will return "Two" i.e. value for the key 2 System.out.println( hashtable.getOrDefault(2, "NotAvailable") ); //this will return "NotAvailable" as the key 4 does not exist System.out.println( hashtable.getOrDefault(4, "NotAvailable") ); |
Output
1 2 |
Two NotAvailable |
How to get the size of the Hashtable using the size method?
The Hashtable size
method returns the number of entries stored in the hashtable object.
1 |
public int size() |
It returns zero if the hashtable is empty.
1 2 3 4 5 6 7 8 9 10 |
Hashtable<Integer, String> ht = new Hashtable<Integer, String>(); //this will return 0 as there are no entries System.out.println("Hashtable size: " + ht.size()); ht.put(1, "One"); ht.put(2, "Two"); //this will return 2 as there are two entries System.out.println("Hashtable size: " + ht.size()); |
Output
1 2 |
Hashtable size: 0 Hashtable size: 2 |
How to check if Hashtable is empty using the isEmpty method?
The Hashtable isEmpty
method returns true if there are no key-value mappings in the hashtable object.
1 |
public boolean isEmpty() |
It returns false if there is at least one key mapped to some value.
1 2 3 4 5 6 7 8 9 10 |
Hashtable<Integer, String> ht = new Hashtable<Integer, String>(); //this will return true as there are no entries System.out.println("Hashtable empty? " + ht.isEmpty()); ht.put(1, "One"); ht.put(2, "Two"); //this will return false as there are two entries System.out.println("Hashtable empty? " + ht.isEmpty()); |
Output
1 2 |
Hashtable empty? true Hashtable empty? false |
How to get all keys stored in the Hashtable using the keys and keySet methods?
The Hashtable keys
method returns an enumeration of the keys contained in this hashtable object.
1 |
public Enumeration<K> keys() |
Once we get a key enumeration, we can iterate through the hashtable keys using the hasMoreElements
and nextElement
methods as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Hashtable<Integer, String> htNumbers = new Hashtable<Integer, String>(); htNumbers.put(1, "One"); htNumbers.put(2, "Two"); htNumbers.put(3, "Three"); /* * To get hashtable keys enumeration, use * the keys method */ Enumeration<Integer> keys = htNumbers.keys(); //iterate hashtable keys while( keys.hasMoreElements() ){ System.out.println( keys.nextElement() ); } |
Output
1 2 3 |
3 2 1 |
The Hashtable keySet
method returns a Set view of all keys contained in this hashtable object.
1 |
public Set<K> keySet() |
The returned Set is a view that is backed by the original Hashtable object. Any changes you make to this set will be reflected in the original hashtable object, and vice versa.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Hashtable<Integer, String> htNumbers = new Hashtable<Integer, String>(); htNumbers.put(1, "One"); htNumbers.put(2, "Two"); htNumbers.put(3, "Three"); /* * To get a set view of all keys of the hashtable, use * the keySet method */ Set<Integer> keys = htNumbers.keySet(); //we can iterate all keys of hashtable using this set's iterator Iterator<Integer> iterator = keys.iterator(); while(iterator.hasNext()){ System.out.println( iterator.next() ); } |
Output
1 2 3 |
3 2 1 |
How to get all values stored in the Hashtable using the elements and values methods?
The Hashtable elements
method returns an enumeration of all the values contained in this hashtable object.
1 |
public Enumeration<V> elements() |
Once we get the hashtable values enumeration, we can iterate through it using the hasMoreElements
and nextElement
methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Hashtable<Integer, String> htNumbers = new Hashtable<Integer, String>(); htNumbers.put(1, "One"); htNumbers.put(2, "Two"); htNumbers.put(3, "Three"); /* * To get hashtable values enumeration, use * the elements method */ Enumeration<String> values = htNumbers.elements(); //iterate hashtable values while( values.hasMoreElements() ){ System.out.println( values.nextElement() ); } |
Output
1 2 3 |
Three Two One |
The Hashtable values
method returns a Collection view of all values contained in this hashtable object.
1 |
public Collection<V> values() |
The returned Collection is a view that is backed by the original Hashtable object. Any changes you make to this collection will be reflected in the original hashtable object, and vice versa.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Hashtable<Integer, String> htNumbers = new Hashtable<Integer, String>(); htNumbers.put(1, "One"); htNumbers.put(2, "Two"); htNumbers.put(3, "Three"); /* * To get a Collection view of all values of the hashtable, use * the values method */ Collection<String> values = htNumbers.values(); //we can iterate all values of hashtable using this collection's iterator Iterator<String> iterator = values.iterator(); while(iterator.hasNext()){ System.out.println( iterator.next() ); } |
Output
1 2 3 |
Three Two One |
How to get all entries (all key-value mappings) from the Hashtable using the entrySet method?
The Hashtable entrySet
method returns a Set view of all the entries stored in this hashtable object.
1 |
public Set<Map.Entry<K,V>> entrySet() |
The entry Set returned by the entrySet
method is backed by the original hashtable object. So, any changes you make to the entry set will be reflected in the original hashtable object, and vice versa.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Hashtable<Integer, String> htNumbers = new Hashtable<Integer, String>(); htNumbers.put(1, "One"); htNumbers.put(2, "Two"); htNumbers.put(3, "Three"); /* * To get all entries stored in the hashtable, use * the entrySet method. */ Set<Map.Entry<Integer, String>> entries = htNumbers.entrySet(); //get an iterator Iterator<Map.Entry<Integer, String>> itr = entries.iterator(); Map.Entry<Integer, String> entry = null; while( itr.hasNext() ){ entry = itr.next(); //get key using getKey method and value using getValue method System.out.println( entry.getKey() + " -> " + entry.getValue() ); } |
Output
1 2 3 |
3 -> Three 2 -> Two 1 -> One |
How to remove mapping(s) from the Hashtable using the remove and clear methods?
The Hashtable remove
method removes the specified key and the value associated with it from the hashtable object.
1 |
public V remove(Object key) |
The remove
method returns the value associated with the key if the key was found and removed from the hashtable. If the key was not found, it returns null.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Hashtable<Integer, String> htNumbers = new Hashtable<Integer, String>(); htNumbers.put(1, "One"); htNumbers.put(2, "Two"); htNumbers.put(3, "Three"); /* * To remove mappings from the hashtable, use * the remove method */ //this will remove 2=>"Two" mapping and return "Two" System.out.println( htNumbers.remove(2) ); //this will return null as the key 4 does not exist System.out.println( htNumbers.remove(2) ); System.out.println("Hashtable contains: " + htNumbers); |
Output
1 2 3 |
Two null Hashtable contains: {3=Three, 1=One} |
The above given remove
method removes the key-value mapping from the hashtable if the specified key is mapped to any value in the hash table object. If you want to remove the mapping if and only if the specified key is mapped to the specified value, use the below given overloaded remove
method.
1 |
public boolean remove(Object key, Object value) |
It removes the entry if the specified key is mapped to the specified value from the hashtable object and returns true. If the specified key is not mapped to the specified value or the specified key does not exist, it returns false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Hashtable<Integer, String> htNumbers = new Hashtable<Integer, String>(); htNumbers.put(1, "One"); htNumbers.put(2, "Two"); htNumbers.put(3, "Three"); /* * The overloaded remove method removes the * mapping if and only if the specified key is * mapped to the specified value */ //this will remove 2="Two" and return true System.out.println( htNumbers.remove(2, "Two")); //this will return false as key 1 is not mapped to "Ten" System.out.println( htNumbers.remove(1, "Ten")); //this will also return false as key 4 does not exist System.out.println( htNumbers.remove(2, "Two")); System.out.println("Hashtable contains: " + htNumbers); |
Output
1 2 3 4 |
true false false Hashtable contains: {3=Three, 1=One} |
The Hashtable clear
method removes all the entries from the hashtable object.
1 |
public void clear() |
The hashtable becomes empty after this method call.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Hashtable<Integer, String> htNumbers = new Hashtable<Integer, String>(); htNumbers.put(1, "One"); htNumbers.put(2, "Two"); htNumbers.put(3, "Three"); /* * To clear or remove all entries, use the * clear method */ htNumbers.clear(); System.out.println( "Is empty? " + htNumbers.isEmpty() ); |
How to replace a value mapped to a key in Hashtable using the replace method?
The Hashtable replace
method replaces the value mapped to the given key in the hashtable object
1 |
public V replace(K key, V value) |
If the specified key exists in the hashtable, it replaces the value with the specified new value and returns the old value. If the key does not exist, it returns null.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Hashtable<Integer, String> htNumbers = new Hashtable<Integer, String>(); htNumbers.put(1, "One"); htNumbers.put(2, "Two"); htNumbers.put(3, "Three"); /* * To replace a value for the key, use * the replace method */ //this will replace "One" with "Ten" and return "One" for key 1 System.out.println( htNumbers.replace(1, "Ten") ); //this will return null as key 4 does not exist System.out.println( htNumbers.replace(4, "Four") ); System.out.println("Hashtable contains: " + htNumbers); |
Output
1 2 3 |
One null Hashtable contains: {3=Three, 2=Two, 1=Ten} |
The above given replace
method replaces the value for a given key if the key is mapped to any value in the hashtable. If you want to replace the value if and only if the specified key is mapped to the specified value, you can use below given overloaded replace
method.
1 |
public boolean replace(K key, V oldValue, V newValue) |
It replaces the old value with the new value for a given key if the key is mapped to the specified old value and returns true. If the key is not mapped to the specified old value, or if the key does not exist, it returns false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Hashtable<Integer, String> htNumbers = new Hashtable<Integer, String>(); htNumbers.put(1, "One"); htNumbers.put(2, "Two"); htNumbers.put(3, "Three"); /* * This will replace "One" with "Ten" and return * true as the key 1 is mapped to "One" */ System.out.println( htNumbers.replace(1, "One", "Ten") ); /* * This will not replace "Two" with "Twenty" and return * false as the key 2 is not mapped to value "Six" */ System.out.println( htNumbers.replace(2, "Six", "Twenty") ); /* * This will return false as the key 4 * does not exist */ System.out.println( htNumbers.replace(4, "Four", "Forty") ); |
Output
1 2 3 4 |
true false false Hashtable contains: {3=Three, 2=Two, 1=Ten} |
How to check if the key exists in the Hashtable using the containsKey method?
The Hashtable containsKey
method returns true if the specified key is mapped to any value in the hashtable object.
1 |
public boolean containsKey(Object key) |
It returns true if the specified object is a key in the hashtable, false otherwise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Hashtable<Integer, String> htNumbers = new Hashtable<Integer, String>(); htNumbers.put(1, "One"); htNumbers.put(2, "Two"); htNumbers.put(3, "Three"); /* * To check if the key exists in the * hashtable, use the containsKey method */ //this will return true as the key 2 exists System.out.println( htNumbers.containsKey(2) ); //this will return false as key 4 does not exist System.out.println( htNumbers.containsKey(4) ); |
Output
1 2 |
true false |
If the Hashtable key is an object of a custom class, then the custom class needs to override the equals
and hashCode
methods for the containsKey
method to work properly.
How to check if the value exists in the Hashtable using the contains and containsValue methods?
The Hashtable contains
method returns true if the specified value is mapped to any key in the hashtable.
1 |
public boolean contains(Object value) |
It returns true if the given value is mapped to one or more keys in the hashtable, false otherwise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
Hashtable<Integer, String> htNumbers = new Hashtable<Integer, String>(); htNumbers.put(1, "One"); htNumbers.put(2, "Two"); htNumbers.put(3, "Three"); htNumbers.put(4, "One"); /* * To check if the given value exists in the * hashtable, use the contains method */ /* * this will return true as the value "One" * is mapped to keys 1 and 4 */ System.out.println( htNumbers.contains("One") ); /* * this will return false as the value "Two" * is mapped to key 2 */ System.out.println( htNumbers.contains("Two") ); /* * this will return false as the value "Four" * is not mapped to any key */ System.out.println( htNumbers.contains("Four") ); |
Output
1 2 3 |
true true false |
The Hashtable containsValue
method is similar to the contains
method in functionality. The contains
method was part of the original Java Hashtable class, but when it was rewritten to implement the Map interface in Java 2, the Hashtable class was required to define the containsValue
method.
If we look at the source code of the Hashtable class, we can see that the containsValue
method calls the contains
method.
1 2 3 |
public boolean containsValue(Object value) { return contains(value); } |
If the Hashtable value is an object of a custom class, then the custom class needs to override the equals
and hashCode
methods for the contains
and containsValue
methods to work properly.
How to clone the Hashtable object using the clone method?
The clone
method of the Hashtable class returns a shallow copy of this hashtable object.
1 |
public Object clone() |
The shallow copy means that only key and value object references are copied, not the actual objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Hashtable<Integer, String> htNumbers = new Hashtable<Integer, String>(); htNumbers.put(1, "One"); htNumbers.put(2, "Two"); htNumbers.put(3, "Three"); /* * To clone the hashtable, use the clone method */ Hashtable<Integer, String> htNumbersClone = (Hashtable<Integer, String>) htNumbers.clone(); System.out.println("Original hashtable: " + htNumbers); System.out.println("Cloned hashtable: " + htNumbersClone); |
Output
1 2 |
Original hashtable: {3=Three, 2=Two, 1=One} Cloned hashtable: {3=Three, 2=Two, 1=One} |
The below given additional Hashtable examples will help you understand the concepts in more detail.
Java Hashtable Examples
References:
Java 8 Hashtable Documentation
Please let me know if you liked the Java Hashtable tutorial with examples in the comments section below.