How to Convert a HashTable to Other Collections Types in Java? Last Updated : 12 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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. Conversion of HashTable to other Collections: There are several types of converting the HashTable to other collections, here are some conversions: HashTable to ArrayList: It allows the retrieval of keys and values in sequential order.HashTable to HashSet: It will remove duplicate values and help us to maintain a unique set of data.HashTable to TreeMap: It sorts the entries based on the custom comparator or natural order.HashTable to LinkedHashMap: This will preserve the insertion order and maintain the data in the order in which elements were added.Program to Convert a HashTable to Other Collections Types in JavaBelow is the implementation of Convert a HashTable to Other Collections Types: Java // Java program to convert a HashTable to other Collections types import java.util.*; public class ExampleConversion { public static void main(String[] args) { // create a HashTable Hashtable<Integer, String> hashTable = new Hashtable<>(); hashTable.put(1, "One"); hashTable.put(2, "Two"); hashTable.put(3, "Three"); // converting HashTable to ArrayList List<Integer> arrayList = new ArrayList<>(hashTable.keySet()); System.out.println("Converted ArrayList Output: " + arrayList); // converting HashTable to HashSet Set<String> hashSet = new HashSet<>(hashTable.values()); System.out.println("Converted HashSet output : " + hashSet); // converting HashTable to TreeMap Map<Integer, String> treeMap = new TreeMap<>(hashTable); System.out.println("Converted TreeMap output : " + treeMap); // converting HashTable to LinkedHashMap Map<Integer, String> linkedHashMap = new LinkedHashMap<>(hashTable); System.out.println("Converted LinkedHashMap output : " + linkedHashMap); } } OutputConverted 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} Explanation of the above Program:In the above example, We have created a Hashtable named hashTable and add some key-value pairs to it.We have converted the Hashtable to different collection types.For ArrayList we have used the keySet() method to get a Set of keys from the Hashtable and then create an ArrayList from this set.For HashSet we have used the values() method to get a Collection of values from the Hashtable and then create a HashSet from this collection.For TreeMap we directly create a TreeMap from the Hashtable.For LinkedHashMap we directly create a LinkedHashMap from the Hashtable.At last, it prints out the converted collection types. Comment More infoAdvertise with us Next Article How to Convert a HashTable to Other Collections Types in Java? S saikumarye6avv Follow Improve Article Tags : Java Java Programs HashTable Java-HashTable Java Examples +1 More Practice Tags : Java Similar Reads How to Use Enumeration to Display Elements of Hashtable in Java? Hashtable class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method. Now here we can get the keys and 2 min read How to Convert a HashSet to JSON in Java? In Java, a HashSet is an implementation of the Set interface that uses a hash table to store elements. It allows fast lookups and does not allow duplicate elements. Elements in a HashSet are unordered and can be of any object type. In this article, we will see how to convert a HashSet to JSON in Jav 2 min read How to Convert ArrayList to HashSet in Java? ArrayList: In Java, ArrayList can have duplicates as well as maintains insertion order. HashSet: HashSet is the implementation class of Set. It does not allow duplicates and uses Hashtable internally. There are four ways to convert ArrayList to HashSet : Using constructor.Using add() method by itera 3 min read How to Convert Two Arrays Containing Keys and Values to HashMap in Java? HashMap is part of the Collections framework of java. It stores the data in the form of key-value pairs. These values of the HashMap can be accessed by using their respective keys or the key-value pairs can be accessed using their indexes (of Integer type). HashMap is similar to Hashtable in java. T 2 min read Converting LinkedHashSet to Different Collection Types in Java In Java, LinkedHashSet is a Class in the Java Collections Framework that extends the Collection HashSet and maintains the insertion order of the elements. The underlined data structure of the LinkedHastSet is that the insertion order is preserved. Converting LinkedHashSet to Other CollectionsIn cert 4 min read Like