Set toArray() Method in Java Last Updated : 06 Feb, 2025 Comments Improve Suggest changes Like Article 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 Comment More infoAdvertise with us Next Article Set toArray() Method in Java gopaldave Follow Improve Article Tags : Java Java-Collections Java-Functions java-set Practice Tags : JavaJava-Collections Similar Reads Set in Java The Set Interface is present in java.util package and extends the Collection interface. It is an unordered collection of objects in which duplicate values cannot be stored. It is an interface that implements the mathematical set. This interface adds a feature that restricts the insertion of duplicat 14 min read Set add() method in Java with Examples The add() method of Set in Java is used to add a specific element into a Set collection. The set add() function adds the element only if the specified element is not already present in the set else the function returns False if the element is already present in the Set. Declaration of add() methodbo 2 min read Set contains() method in Java with Examples The Java.util.Set.contains() method is used to check whether a specific element is present in the Set or not. So basically it is used to check if a Set contains any particular element. Syntax: boolean contains(Object element) Parameters: The parameter element is of the type of Set. This is the eleme 2 min read Set remove() method in Java with Examples The java.util.Set.remove(Object O) method is used to remove a particular element from a Set. Syntax: boolean remove(Object O) Parameters: The parameter O is of the type of element maintained by this Set and specifies the element to be removed from the Set. Return Value: This method returns True if t 1 min read Set addAll() Method in Java In Java, the addAll() method of the Set class is used to add all the elements of a specified collection to the current collection. The elements are added randomly without following any specific order.Example 1: This example demonstrates how to merge two TreeSet using the addAll() method.Java// Java 2 min read Set clear() method in Java with Examples The Java.util.Set.clear() method is used to remove all the elements from a Set. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing Set. Syntax: void clear() Parameters: The me 1 min read Set containsAll() Method in Java The containsAll() method of Set in Java is used to check if a collection contains all the elements of a specified collection. This method is part of the Collection interface. Example 1: This example checks if all elements of one set are present in another set and it will return true if they are iden 2 min read Set hashCode() Method in Java In Java, the hashCode() method is defined in the Object class and is used to generate a hash code for objects. It plays a very important role in hash-based collections like HashMap, HashSet, and HashTable. Example 1: This example demonstrates how hashCode() is used to get the hash code of the HashSe 2 min read Set iterator() method in Java with Examples The java.util.Set.iterator() method is used to return an iterator of the same elements as the set. The elements are returned in random order from what present in the set. Syntax: Iterator iterate_value = Set.iterator(); Parameters: The function does not take any parameter. Return Value: The method i 1 min read Set removeAll() Method in Java In Java, the removeAll() method is part of the Collection interface. It is used to remove all elements from a collection that are present in another collection.Example 1: This example demonstrates how the removeAll() method removes all elements from the first set that are also present in the second 2 min read Like