Get All Entries (mappings) from Hashtable Example

This example shows how to get all the entries (mapping) from the Hashtable in Java. This example also shows how to get all the entries of Hashtable using the entrySet method.

How to get all entries from the Hashtable in Java (all key-value mappings)?

The Hashtable entrySet method returns all the entries stored in the hashtable object.

The entrySet method returns a Set view of all the entries or key-value mappings stored in the hashtable object.

Once we get the entry set using the entrySet method, we can get an iterator from it using the iterator method of the Set. We can then iterate through all the entries of the hashtable using the hasNext and next methods of the iterator.

Each entry in the entry set is an object of the Map.Entry class and represents a single key-value mapping contained in the hashtable object. We can get the key from the entry using the getKey method and value using the getValue method as given below.

Output

Important Note:

The entrySet method returns a view that is backed by the original hashtable object. That means any changes we make to this set will also be reflected in the original hashtable object, and vice versa.

Let’s see an example of that.

Output

As we can see from the output when we changed the entry value for the key 2, the change was reflected in the hashtable object as well.

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

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

References:
Java 8 Hashtable Documentation

About the author

RahimV

RahimV

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 *