Skip to content

Hashtable in Java Tutorial with Examples

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.

Java Hashtable Tutorial

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.

The new object has a default initial capacity of 11 and a default load factor of 0.75.

If you want to create a new hashtable object with custom initial capacity and load factor, you can use the below given overloaded constructor.

The Hashtable class also provides an overloaded constructor that accepts a map object.

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.

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.

Output

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.

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.

Output

The Hashtable putAll method copies all the mappings of the specified map to this hashtable object.

The mappings contained the specified map will replace any values mapped to the matching keys in this hashtable object.

Output

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.

If the specified key does not exist in the hashtable, it returns null.

Output

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.

Output

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.

It returns zero if the hashtable is empty.

Output

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.

It returns false if there is at least one key mapped to some value.

Output

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.

Once we get a key enumeration, we can iterate through the hashtable keys using the hasMoreElements and nextElement methods as given below.

Output

The Hashtable keySet method returns a Set view of all keys contained in this hashtable object.

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.

Output

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.

Once we get the hashtable values enumeration, we can iterate through it using the hasMoreElements and nextElement methods.

Output

The Hashtable values method returns a Collection view of all values contained in this hashtable object.

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.

Output

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.

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.

Output

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.

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.

Output

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.

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.

Output

The Hashtable clear method removes all the entries from the hashtable object.

The hashtable becomes empty after this method call.

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

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.

Output

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.

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.

Output

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.

It returns true if the specified object is a key in the hashtable, false otherwise.

Output

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.

It returns true if the given value is mapped to one or more keys in the hashtable, false otherwise.

Output

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.

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.

The shallow copy means that only key and value object references are copied, not the actual objects.

Output

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.

About the author

Leave a Reply

Your email address will not be published.