Open In App

HashSet toArray(T[]) method in Java with Example

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The toArray(T[]) method method of HashSet class in Java is used to form an array of the same elements as that of the HashSet. It returns an array containing all of the elements in this HashSet in the correct order; the run-time type of the returned array is that of the specified array. If the HashSet fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the run time type of the specified array and the size of this HashSet.
If the HashSet fits in the specified array with room to spare (i.e., the array has more elements than the HashSet), the element in the array immediately following the end of the HashSet is set to null. (This is useful in determining the length of the HashSet only if the caller knows that the HashSet does not contain any null elements.)

Syntax:

public <T> T[] toArray(T[] a)

Parameters: The method accepts one parameter arr[] which is the array into which the elements of the HashSet are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.

Return Value: The method returns an array containing the elements similar to the HashSet.

Exception: The method might throw two types of exception:

  • ArrayStoreException: When the mentioned array is of the different type and is not able to compare with the elements mentioned in the HashSet.
  • NullPointerException: If the array is Null, then this exception is thrown.

Below program illustrates the working of the HashSet.toArray(arr[]) method.

Program 1: When array is of the size of HashSet




// Java code to illustrate toArray(arr[])
  
import java.util.*;
  
public class HashSetDemo {
    public static void main(String args[])
    {
        // Creating an empty HashSet
        HashSet<String>
            hashSet = new HashSet<String>();
  
        // Use add() method to add
        // elements into the HashSet
        hashSet.add("Welcome");
        hashSet.add("To");
        hashSet.add("Geeks");
        hashSet.add("For");
        hashSet.add("Geeks");
  
        // Displaying the HashSet
        System.out.println("The HashSet: "
                           + hashSet);
  
        // Creating the array and using toArray()
        String[] arr = new String[5];
        arr = hashSet.toArray(arr);
  
        // Displaying arr
        System.out.println("The arr[] is:");
        for (int j = 0; j < arr.length; j++)
            System.out.println(arr[j]);
    }
}


Output:

The HashSet: [Geeks, For, Welcome, To]
The arr[] is:
Geeks
For
Welcome
To
null

Program 2: When array is less than the size of HashSet




// Java code to illustrate toArray(arr[])
  
import java.util.*;
  
public class HashSetDemo {
    public static void main(String args[])
    {
        // Creating an empty HashSet
        HashSet<String>
            hashSet = new HashSet<String>();
  
        // Use add() method to add
        // elements into the HashSet
        hashSet.add("Welcome");
        hashSet.add("To");
        hashSet.add("Geeks");
        hashSet.add("For");
        hashSet.add("Geeks");
  
        // Displaying the HashSet
        System.out.println("The HashSet: "
                           + hashSet);
  
        // Creating the array and using toArray()
        String[] arr = new String[1];
        arr = hashSet.toArray(arr);
  
        // Displaying arr
        System.out.println("The arr[] is:");
        for (int j = 0; j < arr.length; j++)
            System.out.println(arr[j]);
    }
}


Output:

The HashSet: [Geeks, For, Welcome, To]
The arr[] is:
Geeks
For
Welcome
To

Program 3: When array is more than the size of HashSet




// Java code to illustrate toArray(arr[])
  
import java.util.*;
  
public class HashSetDemo {
    public static void main(String args[])
    {
        // Creating an empty HashSet
        HashSet<String>
            hashSet = new HashSet<String>();
  
        // Use add() method to add
        // elements into the HashSet
        hashSet.add("Welcome");
        hashSet.add("To");
        hashSet.add("Geeks");
        hashSet.add("For");
        hashSet.add("Geeks");
  
        // Displaying the HashSet
        System.out.println("The HashSet: "
                           + hashSet);
  
        // Creating the array and using toArray()
        String[] arr = new String[10];
        arr = hashSet.toArray(arr);
  
        // Displaying arr
        System.out.println("The arr[] is:");
        for (int j = 0; j < arr.length; j++)
            System.out.println(arr[j]);
    }
}


Output:

The HashSet: [Geeks, For, Welcome, To]
The arr[] is:
Geeks
For
Welcome
To
null
null
null
null
null
null

Program 4: To demonstrate NullPointerException




// Java code to illustrate toArray(arr[])
  
import java.util.*;
  
public class HashSetDemo {
    public static void main(String args[])
    {
        // Creating an empty HashSet
        HashSet<String>
            hashSet = new HashSet<String>();
  
        // Use add() method to add
        // elements into the HashSet
        hashSet.add("Welcome");
        hashSet.add("To");
        hashSet.add("Geeks");
        hashSet.add("For");
        hashSet.add("Geeks");
  
        // Displaying the HashSet
        System.out.println("The HashSet: "
                           + hashSet);
  
        try {
            // Creating the array
            String[] arr = null;
            // using toArray()
            // Since arr is null
            // Hence exception will be thrown
            arr = hashSet.toArray(arr);
  
            // Displaying arr
            System.out.println("The arr[] is:");
            for (int j = 0; j < arr.length; j++)
                System.out.println(arr[j]);
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}


Output:

The HashSet: [Geeks, For, Welcome, To]
Exception: java.lang.NullPointerException


Last Updated : 24 Dec, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads