Open In App

AbstractCollection toArray() Method in Java

Last Updated : 04 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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 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] + " ");
    }
}

Output
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() Method

Copies all the elements from the collection into a new array.

Syntax:

Object[] arr = AbstractCollection.toArray();

  • Parameter: This method does not take any parameter.
  • Return Type: This method returns an array containing the elements of the AbstracrCollection.

Example: This example demonstrates converting a PriorityQueue to an array using the toArray() and printing the array elements.

// 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] + " ");
    }
} 

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

2. 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)

  • Parameter: arr[] is the array into which the elements of the collection will be stored.
  • Return Type: This method returns an array containing the elements of the collection in the correct order.

Exception:

The toArray(arr[]) method may throws two exceptions:

  • ArrayStoreException: If the array type is incompatible with the collection elements.
  • NullPointerException: If the provided array is null.

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 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] + " ");
    }
} 

Output
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.

// 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] + " ");
    }
}

Output
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 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] + " ");
    }
}

Output
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 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);
        }
    }
}

Output
The AbstractCollection: [For, Geeks, Geeks]
Exception: java.lang.NullPointerException


Next Article

Similar Reads

three90RightbarBannerImg