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 returns an array containing the elements similar to the AbstractCollection.

Below programs illustrate the AbstractCollection.toArray() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

The AbstractCollection: [For, Geeks, To, Welcome, Geeks]
The array is:
For
Geeks
To
Welcome
Geeks

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate toArray()
  
import java.util.*;
  
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
        // Creating an empty AbstractCollection
        AbstractCollection<Integer>
            abs_col = new PriorityQueue<Integer>();
  
        // Use add() method to add
        // elements into the AbstractCollection
        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 AbstractCollection
        System.out.println("The AbstractCollection: "
                           + 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]);
    }
}

chevron_right


Output:

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

2. toArray(arr[])

The toArray(arr[]) method method of AbstractCollection class in Java is used to form an array of the same elements as that of the AbstractCollection. It returns an array containing all of the elements in this AbstractCollection in the correct order; the run-time type of the returned array is that of the specified array. If the AbstractCollection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the run time type of the specified array and the size of this AbstractCollection.
If the AbstractCollection fits in the specified array with room to spare (i.e., the array has more elements than the AbstractCollection), the element in the array immediately following the end of the AbstractCollection is set to null. (This is useful in determining the length of the AbstractCollection only if the caller knows that the AbstractCollection does not contain any null elements.)

Syntax:

Object[] arr1 = AbstractCollection.toArray(arr[])

Parameters: The method accepts one parameter arr[] which is the array into which the elements of the AbstractCollection are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.

Return Value: The method returns an array containing the elements similar to the AbstractCollection.



Exception: The method might throw two types of exception:

  • ArrayStoreException: When the mentioned array is of the different type and is not able to compare with the elements mentioned in the AbstractCollection.
  • NullPointerException: If the array is Null, then this exception is thrown.

Below program illustrates the working of the AbstractCollection.toArray(arr[]) method.

Program 1: When array is of the size of AbstractCollection

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate toArray(arr[])
  
import java.util.*;
  
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
        // Creating an empty AbstractCollection
        AbstractCollection<String>
            abs_col = new PriorityQueue<String>();
  
        // Use add() method to add
        // elements into the AbstractCollection
        abs_col.add("Welcome");
        abs_col.add("To");
        abs_col.add("Geeks");
        abs_col.add("For");
        abs_col.add("Geeks");
  
        // Displaying the AbstractCollection
        System.out.println("The AbstractCollection: "
                           + abs_col);
  
        // Creating the array and using toArray()
        String[] arr = new String[5];
        arr = abs_col.toArray(arr);
  
        // Displaying arr
        System.out.println("The arr[] is:");
        for (int j = 0; j < arr.length; j++)
            System.out.println(arr[j]);
    }
}

chevron_right


Output:

The AbstractCollection: [For, Geeks, To, Welcome, Geeks]
The arr[] is:
For
Geeks
To
Welcome
Geeks

Program 2: When array is less than the size of AbstractCollection

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate toArray(arr[])
  
import java.util.*;
  
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
        // Creating an empty AbstractCollection
        AbstractCollection<String>
            abs_col = new PriorityQueue<String>();
  
        // Use add() method to add
        // elements into the AbstractCollection
        abs_col.add("Welcome");
        abs_col.add("To");
        abs_col.add("Geeks");
        abs_col.add("For");
        abs_col.add("Geeks");
  
        // Displaying the AbstractCollection
        System.out.println("The AbstractCollection: "
                           + abs_col);
  
        // Creating the array and using toArray()
        String[] arr = new String[1];
        arr = abs_col.toArray(arr);
  
        // Displaying arr
        System.out.println("The arr[] is:");
        for (int j = 0; j < arr.length; j++)
            System.out.println(arr[j]);
    }
}

chevron_right


Output:

The AbstractCollection: [For, Geeks, To, Welcome, Geeks]
The arr[] is:
For
Geeks
To
Welcome
Geeks

Program 3: When array is more than the size of AbstractCollection

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate toArray(arr[])
  
import java.util.*;
  
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
        // Creating an empty AbstractCollection
        AbstractCollection<String>
            abs_col = new PriorityQueue<String>();
  
        // Use add() method to add
        // elements into the AbstractCollection
        abs_col.add("Welcome");
        abs_col.add("To");
        abs_col.add("Geeks");
        abs_col.add("For");
        abs_col.add("Geeks");
  
        // Displaying the AbstractCollection
        System.out.println("The AbstractCollection: "
                           + abs_col);
  
        // Creating the array and using toArray()
        String[] arr = new String[10];
        arr = abs_col.toArray(arr);
  
        // Displaying arr
        System.out.println("The arr[] is:");
        for (int j = 0; j < arr.length; j++)
            System.out.println(arr[j]);
    }
}

chevron_right


Output:

The AbstractCollection: [For, Geeks, To, Welcome, Geeks]
The arr[] is:
For
Geeks
To
Welcome
Geeks
null
null
null
null
null

Program 4: To demonstrate NullPointerException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate toArray(arr[])
  
import java.util.*;
  
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
        // Creating an empty AbstractCollection
        AbstractCollection<String>
            abs_col = new PriorityQueue<String>();
  
        // Use add() method to add
        // elements into the AbstractCollection
        abs_col.add("Welcome");
        abs_col.add("To");
        abs_col.add("Geeks");
        abs_col.add("For");
        abs_col.add("Geeks");
  
        // Displaying the AbstractCollection
        System.out.println("The AbstractCollection: "
                           + abs_col);
  
        try {
            // Creating the array
            String[] arr = null;
            // using toArray()
            // Since arr is null
            // Hence exception will be thrown
            arr = abs_col.toArray(arr);
  
            // Displaying 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);
        }
    }
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.