Clear or Remove All Entries from Hashtable in Java Example

This example shows how to clear or remove all entries from the Hashtable in Java. The example also shows how to clear or remove all entries using the clear method and iterator.

How to clear or remove all mappings from the Hashtable in Java?

There is a couple of ways using which we can remove all the mappings from the hashtable as given below.

1. Using the clear method

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

The hashtable object becomes empty after this method call.


Please also visit how to check if hashtable is empty example to know more.

2. Using the iterator

We can get all the entries or mappings from the hashtable object using the entrySet method. Once we get the Set view of all the entries, we can obtain an iterator from it using the iterator method.

We can remove the hashtable entries while iterating using the remove method of the iterator as given below.


Instead of iterating the hashtable entries, we can also get all keys of the hashtable using the keySet method and remove the entries using its iterator.

This example is a part of the Java Hashtable Tutorial with Examples.

Please let me know your views in the comments section below.

Java 8 Hashtable Documentation

About the author



My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *

Online Shopping