The toArray() method of Java HashSet is used to form an array of the same elements as that of the HashSet. Basically, it copies all the element from a HashSet to a new array.
Object arr = HashSet.toArray()
Parameters: The method does not take any parameters.
Return Value: The method returns an array containing the elements similar to the HashSet.
Below programs illustrate the HashSet.toArray() method:
The HashSet: [Geeks, For, Welcome, To] The array is: Geeks For Welcome To
The HashSet: [20, 5, 25, 10, 30, 15] The array is: 20 5 25 10 30 15
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.