Convert HashSet to Array in Java with Example

In this post, we will convert HashSet to an Array. In the last tutorial, we have learnt how to iterate over a HashSet/Set. There are two ways to achieve our goal of converting HashSet to an Array in Java:

1. Using toArray() method [Recommended]

2. Traverse the HashSet elements and add them to Array object

Let's dive deep into the topic:

Read Also: Convert HashSet to List in Java

Program to Convert HashSet to Array in Java

1. Using toArray() method


The easiest way to convert HashSet to Array in Java is by using toArray() method as shown below in the example:

 import java.util.*;

public class HashSetToArray {
    public static void main(String args[]) {
      
      // Create a HashSet object
      HashSet<String> hashset = new HashSet<String>();
      
      // Adding elements to HashSet object
      hashset.add("Doctor");
      hashset.add("Engineer");
      hashset.add("Lawyer");
      hashset.add("Police");
      
      // Printing HashSet elements 
      System.out.println("HashSet contains: "+ hashset);
      
      // Creating an Array of HashSet size
      String[] array = new String[hashset.size()];
      
      // Converting HashSet to Array using toArray() method
      hashset.toArray(array);
      
      // Printing Array elements
      System.out.println("Array contains: ");
      for (String str : array) {
          System.out.println(str);
      }
    }
}


Output:

HashSet contains: [Engineer, Doctor, Lawyer, Police]
Array contains:
Engineer
Doctor
Lawyer
Police

2. Traverse the HashSet elements and add them to the Array object


The another way to convert HashSet to Array in Java is by traversing the HashSet elements and add them to the Array object as shown below in the example:

 import java.util.*;

public class HashSetToArray2 {
    public static void main(String args[]) {
      
      // Creating a HashSet object
      HashSet<String> hashset2 = new HashSet<String>();
      
      // Adding elements to HashSet object
      hashset2.add("Soccer");
      hashset2.add("Football");
      hashset2.add("Baseball");
      hashset2.add("Basketball");
      
      // Displaying HashSet elements 
      System.out.println("HashSet object contains: "+ hashset2);
      
      // Initializing an Array of HashSet size
      String[] array = new String[hashset2.size()];
      int index = 0;
      
      // Iterating over the HashSet and adding elements to the Array
      for (String element : hashset2)
      {
          array[index++] = element;
      }
      
      // Displaying Array elements
      System.out.println("Array object contains: ");
      for (String str: array) {
          System.out.println(str);
      }
    }
}


Output:

HashSet object contains: [Baseball, Soccer, Basketball, Football]
Array object contains:
Baseball
Soccer
Basketball
Football


That's all for today. Please mention in the comments if you have any questions related to how to convert HashSet to Array in Java with example.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry