Skip to content

Convert Hashtable to ArrayList in Java Example

This example shows how to convert Hashtable to ArrayList in Java. This example also shows how to convert Hashtable keys, values, or entries to the ArrayList object.

How to covert Hashtable to an ArrayList in Java?

The Hashtable is an implementation of the Map interface that maps keys to values while the ArrayList class in Java is an implementation of the List interface that is a dynamic array of elements. These two are different types of data structures and cannot be converted directly to each other.

However, we can convert Hashtable keys, values, or entries to an ArrayList as given below.

How to convert Hashtable keys to an ArrayList?

The keySet method of the Hashtable class returns a Set view of all the keys contained in the hashtable object. Once we get the set of keys, we can convert it to an object of the ArrayList class using the constructor as given below.

Output

How to convert Hashtable values to an ArrayList?

The Hashtable values method returns a Collection view of all the values contained in the hash table object. Once we get the Collection object, we can then convert it to an ArrayList using the constructor as given below.

Output

How to convert Hashtable entries to an ArrayList?

The Hashtable entrySet method returns a Set view of all the entries or mappings contained in the hash table object. Once we get the entries, we can convert it to an ArrayList using the constructor as given below.

Output

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

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

References:
Java 8 Hashtable Documentation

About the author

Leave a Reply

Your email address will not be published.