HashSet toArray() method in Java with Example Last Updated : 24 Dec, 2018 Comments Improve Suggest changes Like Article Like Report 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. Syntax: 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: Program 1: Java // Java code to illustrate toArray() import java.util.*; public class HashSetDemo { public static void main(String args[]) { // Creating an empty HashSet HashSet<String> abs_col = new HashSet<String>(); // Use add() method to add // elements into the HashSet abs_col.add("Welcome"); abs_col.add("To"); abs_col.add("Geeks"); abs_col.add("For"); abs_col.add("Geeks"); // Displaying the HashSet System.out.println("The HashSet: " + abs_col); // Creating the array and using toArray() Object[] arr = abs_col.toArray(); System.out.println("The array is:"); for (int j = 0; j < arr.length; j++) System.out.println(arr[j]); } } Output: The HashSet: [Geeks, For, Welcome, To] The array is: Geeks For Welcome To Program 2: Java // Java code to illustrate toArray() import java.util.*; public class HashSetDemo { public static void main(String args[]) { // Creating an empty HashSet HashSet<Integer> abs_col = new HashSet<Integer>(); // Use add() method to add // elements into the HashSet abs_col.add(10); abs_col.add(15); abs_col.add(30); abs_col.add(20); abs_col.add(5); abs_col.add(25); // Displaying the HashSet System.out.println("The HashSet: " + abs_col); // Creating the array and using toArray() Object[] arr = abs_col.toArray(); System.out.println("The array is:"); for (int j = 0; j < arr.length; j++) System.out.println(arr[j]); } } Output: The HashSet: [20, 5, 25, 10, 30, 15] The array is: 20 5 25 10 30 15 Comment More infoAdvertise with us Next Article HashSet toArray() method in Java with Example C code_r Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions java-hashset +1 More Practice Tags : JavaJava-Collections Similar Reads HashSet toArray(T[]) method in Java with Example 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 HashSe 4 min read Vector toArray() Method in Java with Examples 1. toArray() The toArray() method of Vector class in Java is used to form an array of the same elements as that of the Vector. Basically, it copies all the element from a Vector to a new array. Syntax: Object[] arr = Vector.toArray() Parameters: The method does not take any parameters. Return Value: 5 min read Stack toArray() method in Java with Example The toArray() method of Stack class in Java is used to form an array of the same elements as that of the Stack. Basically, it copies all the element from a Stack to a new array. Syntax: Object[] arr = Stack.toArray() Parameters: The method does not take any parameters. Return Value: The method retur 2 min read TreeSet hashCode() method in Java with Example The hashCode() method of TreeSet in Java is used to get the hashCode value for this instance of the TreeSet. It returns an integer value which is the hashCode value for this instance of the TreeSet. Syntax: public int hashCode() Parameters: This function has no parameters. Returns: The method return 2 min read SortedMap hashCode() method in Java with Examples The hashCode() method of SortedMap interface in Java is used to generate a hashCode for the given map containing key and values. Syntax: int hashCode() Parameters: This method has no argument. Returns: This method returns the hashCode value for the given map. Note: The hashCode() method in SortedMap 2 min read ArrayList toArray() method in Java with Examples The toArray() method of ArrayList is used to return an array containing all the elements in ArrayList in the correct order.Declaring toArray() methodpublic Object[] toArray() or public <T> T[] toArray(T[] a)Parameters: This method either accepts no parameters or it takes an array T[] as a para 3 min read SortedSet toArray() method in Java with Examples The toArray() method of Java SortedSet is used to form an array of the same elements as that of the SortedSet. Basically, it copies all the element from a SortedSet to a new array. Syntax: Object[] toArray() Parameters: The method does not take any parameters. Return Value: The method returns an arr 2 min read TreeSet toArray() method in Java with Example The toArray() method of Java TreeSet is used to form an array of the same elements as that of the TreeSet. Basically, it copies all the element from a TreeSet to a new array. Syntax: Object[] arr = TreeSet.toArray() Parameters: The method does not take any parameters. Return Value: The method return 2 min read Stack toArray(T[]) method in Java with Example The toArray(T[]) method method of Stack class in Java is used to form an array of the same elements as that of the Stack. It returns an array containing all of the elements in this Stack in the correct order; the run-time type of the returned array is that of the specified array. If the Stack fits i 4 min read HashSet hashCode() method in Java with Example The hashCode() method of HashSet in Java is used to get the hashCode value for this instance of the HashSet. It returns an integer value which is the hashCode value for this instance of the HashSet. Syntax: public int hashCode() Parameters: This function has no parameters. Returns: The method return 2 min read Like