Set to Array in Java Last Updated : 29 Mar, 2024 Comments Improve Suggest changes Like Article Like Report Given a set (HashSet or TreeSet) of strings in Java, convert it into an array of strings. Input : Set hash_Set = new HashSet(); hash_Set.add("Geeks"); hash_Set.add("For"); Output : String arr[] = {"Geeks", "for"} Method 1 (Simple) We simply create an empty array. We traverse the given set and one by one add elements to the array. Java // Java program to demonstrate conversion of // Set to array using simple traversal import java.util.*; class Test { public static void main(String[] args) { // Creating a hash set of strings Set<String> s = new HashSet<String>(); s.add("Geeks"); s.add("for"); int n = s.size(); String arr[] = new String[n]; int i = 0; for (String x : s) arr[i++] = x; System.out.println(Arrays.toString(arr)); } } Output: [Geeks, for] Method 2 (Using System.arraycopy()) Java // Java program to demonstrate conversion of // Set to array using simple traversal import java.util.*; class Test { public static void main(String[] args) { // Creating a hash set of strings Set<String> s = new HashSet<String>(); s.add("Geeks"); s.add("for"); int n = s.size(); String arr[] = new String[n]; // Copying contents of s to arr[] System.arraycopy(s.toArray(), 0, arr, 0, n); System.out.println(Arrays.toString(arr)); } } Output: [Geeks, for] Method 3 (Using toArray()) Java // Java program to demonstrate conversion of // Set to array using toArray() import java.util.*; class Test { public static void main(String[] args) { // Creating a hash set of strings Set<String> s = new HashSet<String>(); s.add("Geeks"); s.add("for"); int n = s.size(); String arr[] = new String[n]; // Please refer below post for syntax // details of toArray() // https://www.geeksforgeeks.org/arraylist-array-conversion-java-toarray-methods/ arr = s.toArray(arr); System.out.println(Arrays.toString(arr)); } } Output: [Geeks, for] Method 4 (Using Arrays.copyOf()) Java // Java program to demonstrate conversion of // Set to array using Arrays.copyOf() import java.util.*; class Test { public static void main(String[] args) { // Creating a hash set of strings Set<String> s = new HashSet<String>(); s.add("Geeks"); s.add("for"); String[] arr = Arrays.copyOf(s.toArray(), s.size(), String[].class); System.out.println(Arrays.toString(arr)); } } Output: [Geeks, for] Method 5 (Using stream in Java) We use stream in Java to convert given set to steam, then stream to array. This works only in Java 8 or versions after that. Java // Java program to demonstrate conversion of // Set to array using stream import java.util.*; class Test { public static void main(String[] args) { // Creating a hash set of strings Set<String> s = new HashSet<String>(); s.add("Geeks"); s.add("for"); int n = s.size(); String[] arr = s.stream().toArray(String[] ::new); System.out.println(Arrays.toString(arr)); } } Output: [Geeks, for] Comment More infoAdvertise with us Next Article Set to Array in Java kartik Follow Improve Article Tags : Misc Java Java-Collections Java-Strings Java-Arrays java-treeset java-hashset java-set Java-Array-Programs Java-Set-Programs +6 More Practice Tags : JavaJava-CollectionsJava-StringsMisc Similar Reads Array set() method in Java The java.lang.reflect.Array.set() is an inbuilt method in Java and is used to set a specified value to a specified index of a given object array. Syntax Array.set(Object []array, int index, Object value) Parameter : array : This is an array of type Object which is to be updated. index : This is the 3 min read Array setLong() method in Java The java.lang.reflect.Array.setLong() is an inbuilt method in Java and is used to set a specified long value to a specified index of a given object array. Syntax: Array.setLong(Object []array, int index, long value) Parameter: array : This is an array of type Object which is to be updated. index : T 3 min read String Arrays in Java A String Array in Java is an array that stores string values. The string is nothing but an object representing a sequence of char values. Strings are immutable in Java, this means their values cannot be modified once created.When we create an array of type String in Java, it is called a String Array 5 min read Array setShort() method in Java The java.lang.reflect.Array.setShort() is an inbuilt method in Java and is used to set a specified short value to a specified index of a given object array. Syntax: Array.setShort(Object []array,int index, short value) Parameters: This method takes 3 parameters: array: This is an array of type Objec 3 min read Convert List to Array in Java The List interface provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects where duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. Now here we are give 5 min read Array setInt() method in Java The java.lang.reflect.Array.setInt() is an inbuilt method in Java and is used to set a specified int value to a specified index of a given object array. Syntax: Array.setInt(Object []array, int index, int value) Parameter: array: This is an array of type Object which is to be updated. index: This is 3 min read Array get() Method in Java The java.lang.reflect.Array.get() is an inbuilt method in Java and is used to return the element at a given index from the specified Array. Syntax Array.get(Object []array, int index) Parameters : This method accepts two mandatory parameters: array: The object array whose index is to be returned. in 3 min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Array setDouble() method in Java The java.lang.reflect.Array.setDouble() is an inbuilt method in Java and is used to set a specified double value to a specified index of a given object array. Syntax: Array.setDouble(Object []array, int index, double value) Parameter: This method takes three parameters: array: This is an array of ty 3 min read How to Return an Array in Java? An array is a data structure that consists of a group of elements of the same data type such that each element of the array can be identified by a single array index or key. The elements of the array are stored in a way that the address of any of the elements can be calculated using the location of 5 min read Like