Last Updated : 11 Jul, 2025
In Java, the toArray() method is the part of the AbstractCollection class. It is used to convert collections like List, Set, etc. into an array. It copies all the elements from the collection into a new array.
Example:
Java
// Java Program to dmeosntrates the
// working of toArray() of type String
import java.util.*;
public class Geeks {
public static void main(String args[]) {
// Creating an empty AbstractCollection
AbstractCollection<String> a = new PriorityQueue<String>();
// Adding elements into the AbstractCollection
a.add("Java");
a.add("C");
a.add("C++");
a.add("JS");
// Displaying the AbstractCollection
System.out.println("The AbstractCollection: " + a);
// Creating the array and using toArray()
Object[] arr = a.toArray();
System.out.println("The array is:");
for (int j = 0; j < arr.length; j++)
System.out.print(arr[j] + " ");
}
}
The AbstractCollection: [C, JS, C++, Java] The array is: C JS C++ Java
Now, there are two versions of toArray() method i.e. one without parameter and one with the parameter.
1. toArray() MethodCopies all the elements from the collection into a new array.
Syntax:
Object[] arr = AbstractCollection.toArray();
Example: This example demonstrates converting a PriorityQueue to an array using the toArray() and printing the array elements.
Java
// Java program to demonstrate the
// working of toArray() of type integer
import java.util.*;
public class Geeks {
public static void main(String args[])
{
// Creating an empty AbstractCollection
AbstractCollection<Integer>
a = new PriorityQueue<Integer>();
// Use add() method to add
// elements into the AbstractCollection
a.add(10);
a.add(15);
a.add(30);
a.add(20);
a.add(5);
a.add(25);
// Displaying the AbstractCollection
System.out.println("The AbstractCollection: "
+ a);
// Creating the array and using toArray()
Object[] arr = a.toArray();
System.out.println("The array is:");
for (int j = 0; j < arr.length; j++)
System.out.print(arr[j] + " ");
}
}
The AbstractCollection: [5, 10, 25, 20, 15, 30] The array is: 5 10 25 20 15 302. toArray(arr[]) Method
In Java, toArray(arr[]) method is used to covert an AbstractCollection into an array of specified type. It fills the provided array with the elements of the collection. If the provided array is large enough it is used directly otherwise, a new array of the appropriate type and size is created.
Syntax:
Object[] toArray(T[] arr)
Exception:
The toArray(arr[]) method may throws two exceptions:
Example 1: This example demonstrates how to convert an AbstractCollection to an array using toArray(arr[]) method when the provided array is of the same size as the collections.
Java
// Java program to demonstrates
// the working of toArray(arr[])
import java.util.*;
public class Geeks {
public static void main(String args[])
{
// Creating an empty AbstractCollection
AbstractCollection<String>
a = new PriorityQueue<String>();
// Use add() method to add
// elements into the AbstractCollection
a.add("Geeks");
a.add("For");
a.add("Geeks");
// Displaying the AbstractCollection
System.out.println("The AbstractCollection: "
+ a);
// Creating the array and using toArray()
String[] arr = new String[3];
arr = a.toArray(arr);
System.out.println("The Array is:");
for (int j = 0; j < arr.length; j++)
System.out.print(arr[j] + " ");
}
}
The AbstractCollection: [For, Geeks, Geeks] The Array is: For Geeks Geeks
Example 2: This example demonstrates how the toArray(arr[]) method in java automatically resizes the array when it is smaller than the AbstractCollection size.
Java
// C# program to demonstrates how
// toArray(arr[]) resized automatically
import java.util.*;
public class Geeks {
public static void main(String args[]) {
// Creating an empty AbstractCollection
AbstractCollection<String> a = new PriorityQueue<String>();
// Adding elements into the AbstractCollection
a.add("Geeks");
a.add("For");
a.add("Geeks");
a.add("Java");
// Displaying the AbstractCollection
System.out.println("The AbstractCollection: " + a);
// Creating an array smaller than
// the size of AbstractCollection
String[] arr = new String[3];
arr = a.toArray(arr);
System.out.println("The Array is:");
for (int j = 0; j < arr.length; j++)
System.out.print(arr[j] + " ");
}
}
The AbstractCollection: [For, Geeks, Geeks, Java] The Array is: For Geeks Geeks Java
Example 3: This example demonstrates how the toArrray(arr[]) method in java copies element from an AbstractCollection to an array, even when the array is larger than the collection.
Java
// Java program to demonstrates how
// toArray(arr[]) works when array
// size is more than the size of AbstractCollection
import java.util.*;
public class Geeks {
public static void main(String args[])
{
// Creating an empty AbstractCollection
AbstractCollection<String> a
= new PriorityQueue<String>();
// Adding elements into the AbstractCollection
a.add("Geeks");
a.add("For");
a.add("Geeks");
a.add("Java");
// Displaying the AbstractCollection
System.out.println("The AbstractCollection: " + a);
// Creating an array smaller than the size of
// AbstractCollection
String[] arr = new String[6];
arr = a.toArray(arr);
System.out.println("The Array is:");
for (int j = 0; j < arr.length; j++)
System.out.print(arr[j] + " ");
}
}
The AbstractCollection: [For, Geeks, Geeks, Java] The Array is: For Geeks Geeks Java null null
Example 4: This example demonstrates that passing a null array to the toArray() method in Java throw a NullPointerException.
Java
// Java program to demonstrates how
// toArray(arr[]) thows NullPointerException
import java.util.*;
public class Geeks {
public static void main(String args[])
{
// Creating an empty AbstractCollection
AbstractCollection<String>
a = new PriorityQueue<String>();
// Use add() method to add
// elements into the AbstractCollection
a.add("Geeks");
a.add("For");
a.add("Geeks");
// Displaying the AbstractCollection
System.out.println("The AbstractCollection: "
+ a);
try {
// Creating the array
String[] arr = null;
// using toArray()
// Since arr is null
// Hence exception will be thrown
arr = a.toArray(arr);
System.out.println("The arr[] is:");
for (int j = 0; j < arr.length; j++)
System.out.println(arr[j]);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
The AbstractCollection: [For, Geeks, Geeks] Exception: java.lang.NullPointerException
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4