Set toArray() Method in Java Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report In Java, the toArray() method is used to convert a collection to an array. It returns an array containing all the elements in the collection in the correct order.Example 1: Converting a Set to an ArrayThis is an example where a Set of String elements is converted into an array using the toArray() method. The method returns an Object[] array. Java // Java Program to demonstrates the working toArray() import java.util.*; public class Geeks { public static void main(String args[]) { // Creating a Set of Strings Set<String> s = new HashSet<String>(); // Adding elements to the Set s.add("Java"); s.add("C++"); s.add("C"); System.out.println("The Set: " + s); // Converting the Set to an Object array Object[] arr = s.toArray(); System.out.println("The Array is:"); for (Object obj : arr) { System.out.print(obj + " "); } } } OutputThe Set: [Java, C++, C] The Array is: Java C++ C Syntax of toArray() MethodObject[] toArray();Parameter: This method does not take any parameterReturn Type: This method returns an array of type object which contains all the elements of the collection in the correct order.Example 2: Converting a Set of Integers to a Specific Type ArrayIn this example, we use the toArray(T[] a) method, which allows us to specify the type of the array to be returned. This avoids the need for casting when using the array. Java // Converting set to an integer array import java.util.*; public class Geeks { public static void main(String args[]) { // Creating a Set of Integers Set<Integer> s = new HashSet<Integer>(); // Adding elements to the Set s.add(10); s.add(15); s.add(30); s.add(20); s.add(5); s.add(25); System.out.println("The Set: " + s); // Converting the Set to an Integer array Integer[] arr = s.toArray(new Integer[0]); System.out.println("The Array is:"); for (Integer num : arr) { System.out.println(num); } } } OutputThe Set: [20, 5, 25, 10, 30, 15] The Array is: 20 5 25 10 30 15 Create Quiz Comment G gopaldave Follow 1 Improve G gopaldave Follow 1 Improve Article Tags : Java Java-Collections Java-Functions java-set Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like