Open In App

How to Convert a HashTable to Other Collections Types in Java?

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
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 Java

Below 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);
    }
}


Output

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}

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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads