![]() |
VOOZH | about |
In Java, a Hashtable is a data structure that stores the data in the form of key and value pairs. And each key is mapped to a specific value. It implements the Map interface. HashTable provides a simple way to access the data using the unique keys.
In this article, we will learn how to convert a HashTable to other Collection types in Java.
Note: HashTable does not allow NULL keys or NULL values.
There are several types of converting the HashTable to other collections, here are some conversions:
Below is the implementation of Convert a HashTable to Other Collections Types:
Converted ArrayList Output: [3, 2, 1]
Converted HashSet output : [One, Two, Three]
Converted TreeMap output : {1=One, 2=Two, 3=Three}
Converted LinkedHashMap output : {3=Three, 2=Two, 1=One}
In the above example,
hashTable and add some key-value pairs to it.keySet() method to get a Set of keys from the Hashtable and then create an ArrayList from this set.values() method to get a Collection of values from the Hashtable and then create a HashSet from this collection.