The Wayback Machine - https://web.archive.org/web/20241119080017/https://www.geeksforgeeks.org/set-toarray-method-in-java-with-example/
Open In App

Set toArray() method in Java with Example

Last Updated : 24 Aug, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The toArray() method of Java Set is used to form an array of the same elements as that of the Set. Basically, it copies all the element from a Set to a new array.
Syntax: 
 

Object[] toArray()

Parameters: The method takes optional parameter. If we provide the type of Object array we want we can pass it as an argument. for ex: set.toArray(new Integer[0]) returns an array of type Integer, we can also do it as set.toArray(new Integer[size]) where size is the size of the resultant array. Doing it in the former way works as the required size is allocated internally. 
Return Value: The method returns an array containing the elements similar to the Set.
Below programs illustrate the Set.toArray() method:
Program 1:
 

Java




// Java code to illustrate toArray()
 
import java.util.*;
 
public class SetDemo {
    public static void main(String args[])
    {
        // Creating an empty Set
        Set<String> abs_col = new HashSet<String>();
 
        // Use add() method to add
        // elements into the Set
        abs_col.add("Welcome");
        abs_col.add("To");
        abs_col.add("Geeks");
        abs_col.add("For");
        abs_col.add("Geeks");
 
        // Displaying the Set
        System.out.println("The Set: "
                           + 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 Set: [Geeks, For, Welcome, To]
The array is:
Geeks
For
Welcome
To

 

Program 2:
 

Java




// Java code to illustrate toArray()
 
import java.util.*;
 
public class SetDemo {
    public static void main(String args[])
    {
        // Creating an empty Set
        Set<Integer> abs_col = new HashSet<Integer>();
 
        // Use add() method to add
        // elements into the Set
        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 Set
        System.out.println("The Set: " + abs_col);
 
        // Creating the array and using toArray()
        Integer[] arr = abs_col.toArray(new Integer[0]);
 
        System.out.println("The array is:");
        for (int j = 0; j < arr.length; j++)
            System.out.println(arr[j]);
    }
}


Output: 

The Set: [20, 5, 25, 10, 30, 15]
The array is:
20
5
25
10
30
15

 

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#toArray()
 



Previous Article
Next Article

Similar Reads

LinkedHashSet toArray(T[]) method in Java with Example
The toArray(T[]) method method of LinkedHashSet class in Java is used to form an array of the same elements as that of the LinkedHashSet. It returns an array containing all of the elements in this LinkedHashSet in the correct order; the run-time type of the returned array is that of the specified array. If the LinkedHashSet fits in the specified ar
4 min read
LinkedHashSet toArray() method in Java with Example
The toArray() method of Java LinkedHashSet is used to form an array of the same elements as that of the LinkedHashSet. Basically, it copies all the element from a LinkedHashSet to a new array. Syntax: Object[] arr = LinkedHashSet.toArray() Parameters: The method does not take any parameters. Return Value: The method returns an array containing the
2 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 returns an array containing the elements similar to the
2 min read
HashSet toArray() method in Java with Example
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
2 min read
AbstractSet toArray() method in Java with Example
The toArray() method of Java AbstractSet is used to form an array of the same elements as that of the AbstractSet. Basically, it copies all the element from a AbstractSet to a new array. Syntax: Object[] arr = AbstractSet.toArray() Parameters: The method does not take any parameters. Return Value: The method returns an array containing the elements
2 min read
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 HashSet fits in the specified array, it is returned ther
4 min read
AbstractSet toArray(T[]) method in Java with Example
The toArray(T[]) method method of AbstractSet class in Java is used to form an array of the same elements as that of the AbstractSet. It returns an array containing all of the elements in this AbstractSet in the correct order; the run-time type of the returned array is that of the specified array. If the AbstractSet fits in the specified array, it
4 min read
AbstractSequentialList toArray() method in Java with Example
The toArray() method of Java AbstractSequentialList is used to form an array of the same elements as that of the AbstractSequentialList. Basically, it copies all the element from a AbstractSequentialList to a new array. Syntax: Object[] arr = AbstractSequentialList.toArray() Parameters: The method does not take any parameters. Return Value: The met
2 min read
AbstractSequentialList toArray(T[]) method in Java with Example
The toArray(arr[]) method of AbstractSequentialList class in Java is used to form an array of the same elements as that of the AbstractSequentialList. It returns an array containing all of the elements in this AbstractSequentialList in the correct order; the run-time type of the returned array is that of the specified array. If the AbstractSequenti
4 min read
LinkedBlockingDeque toArray() method in Java with Example
toArray() The Java.util.concurrent.LinkedBlockingDeque.toArray() method returns an array containing all the elements in the deque in proper sequence i.e. from first to last. The returned array will be safe as a new array is created (hence new memory is allocated). Thus the caller is free to modify the array. It acts as a bridge between array-based
3 min read
CopyOnWriteArraySet toArray() method in Java with Example
toArray() The Java.util.concurrent.CopyOnWriteArraySet.toArray() method returns an array containing all the elements in the set in proper sequence i.e. from first to last. The returned array will be safe as a new array is created (hence new memory is allocated). Thus the caller is free to modify the array. It acts as a bridge between array-based an
3 min read
ConcurrentLinkedDeque toArray() method in Java with Example
toArray() The Java.util.concurrent.ConcurrentLinkedDeque.toArray() method returns an array containing all the elements in the deque in proper sequence i.e. from first to last. The returned array will be safe as a new array is created (hence new memory is allocated). Thus the caller is free to modify the array. It acts as a bridge between array-base
3 min read
TreeSet toArray(T[]) method in Java with Example
The toArray(T[]) method method of TreeSet class in Java is used to form an array of the same elements as that of the TreeSet. It returns an array containing all of the elements in this TreeSet in the correct order; the run-time type of the returned array is that of the specified array. If the TreeSet fits in the specified array, it is returned ther
4 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 returns an array containing the elements similar to the
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 in the specified array, it is returned therein. Oth
4 min read
LinkedList toArray() method in Java with Example
java.util.LinkedList.toArray() method is used to convert LinkedList into an Array. It returns the same LinkedList elements but in the form of Array only. We have two method to convert LinkedList into an Array toArray() - without parametertoArray(arrayName) - with parameterMethod-1: toArray() - without parameter The Java.util.LinkedList.toArray() me
4 min read
PriorityQueue toArray() Method in Java
The java.util.PriorityQueue.toArray() method in Java is used to form an array of the same elements as that of the Priority Queue. Basically, it copies all the element from a priority queue to a new array. Syntax: Object[] arr = Priority_Queue.toArray() Parameters: The method does not take any parameters. Return Value: The method returns an array co
3 min read
ArrayDeque toArray() Method in Java
The java.util.ArrayDeque.toArray() method is used to form an array of the same elements as that of the Deque. Basically, the method copies all the element from this deque to a new array. Syntax: Object[] arr = Array_Deque.toArray() Parameters: The method does not take any parameters. Return Value: The method returns an array containing the elements
2 min read
ArrayBlockingQueue toArray() Method in Java
The toArray() method of ArrayBlockingQueue class: The toArray() method of ArrayBlockingQueue class is used to create an array containing the same elements as that of this ArrayBlockingQueue, in proper sequence. Basically, it copies all the element from the ArrayBlockingQueue to a new array. This method behaves as a bridge between array and collecti
3 min read
LinkedBlockingQueue toArray() method in Java
toArray() The toArray method of LinkedBlockingQueue is used to create an array having the same elements as that of this LinkedBlockingQueue, in proper sequence. Actually, this method copies all the elements from the LinkedBlockingQueue to a new array. This method behaves as a bridge between array and LinkedBlockingQueue. Syntax: public Object[] toA
5 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: The method returns an array containing elements s
5 min read
PriorityBlockingQueue toArray() method in Java
toArray() The toArray method of PriorityBlockingQueue is used to create an array having the same elements as that of this PriorityBlockingQueue, in proper sequence. Actually, this method copies all the element from the PriorityBlockingQueue to a new array. This method behaves as a bridge between array and PriorityBlockingQueue. Syntax: public Objec
5 min read
ConcurrentLinkedQueue toArray() Method in Java
toArray() : The toArray() method of ConcurrentLinkedQueue is used to returns an array of the same elements as that of the ConcurrentLinkedQueue in proper sequence. Basically, it copies all the element from a ConcurrentLinkedQueue to a new array. This method behaves as a bridge between array and ConcurrentLinkedQueue. Syntax: public Object[] toArray
4 min read
AbstractCollection toArray() Method in Java with Examples
1. toArray() The toArray() method of Java AbstractCollection is used to form an array of the same elements as that of the AbstractCollection. Basically, it copies all the element from a AbstractCollection to a new array. Syntax: Object[] arr = AbstractCollection.toArray() Parameters: The method does not take any parameters. Return Value: The method
5 min read
CopyOnWriteArrayList toArray() method in Java
The toArray() method of CopyOnWriteArrayList is used to return an array containing all the elements in CopyOnWriteArrayList in the correct order. Syntax: public Object[] toArray() or public <T> T[] toArray(T[] a) Parameters: This method either accepts no parameters or it takes an array T[] a as parameter which is the array into which the elem
2 min read
DelayQueue toArray() method in Java with Examples
The toArray() method of DelayQueue is used to return an array containing all the elements in DelayQueue. There elements are not in any specific order in the array.Syntax: public Object[] toArray () or public T[] toArray (T[] a) Parameters: This method either accepts no parameters or it takes an array T[] a as parameter which is the array into which
4 min read
Java Guava | Booleans.toArray() method with Examples
The toArray() method of Booleans Class in the Guava library is used to convert the boolean values, passed as the parameter to this method, into a Boolean Array. These boolean values are passed as a Collection to this method. This method returns a Boolean array. Syntax: public static boolean[] toArray(Collection<Boolean> collection) Parameters
2 min read
Java Guava | Bytes.toArray() method with Examples
The toArray() method of Bytes Class in the Guava library is used to convert the byte values, passed as the parameter to this method, into a Byte Array. These byte values are passed as a Collection to this method. This method returns a Byte array. Syntax: public static byte[] toArray(Collection<? extends Number> collection) Parameters: This me
2 min read
Java Guava | Longs.toArray() method with Examples
The toArray() method of Longs Class in the Guava library is used to convert the long values, passed as the parameter to this method, into a Long Array. These long values are passed as a Collection to this method. This method returns a Long array. Syntax: public static long[] toArray(Collection<? extends Number> collection) Parameters: This me
2 min read
Java Guava | Chars.toArray() method with Examples
The toArray() method of Chars Class in the Guava library is used to convert the char values, passed as the parameter to this method, into a Char Array. These char values are passed as a Collection to this method. This method returns a Char array. Syntax: public static char[] toArray(Collection<Character> collection) Parameters: This method ac
2 min read
three90RightbarBannerImg