Java Collection toArray() Method Last Updated : 29 Dec, 2023 Comments Improve Suggest changes Like Article Like Report The toArray() method of Java Collection returns an array containing elements that are inside the calling collection. This article will discuss the toArray() method, its syntaxes, how it works, and some code examples. Syntax of toArray() MethodObject[] toArray();Return Type: The return type of the above syntax is Object[] (Array). Examples of Java toArray() MethodThe example given below returns an array of type Object containing elements as of list1. We use this syntax when we don't want a particular return type. Below is the implementation of the above method: Java // Java Program to Java toArray() Method import java.io.*; import java.util.ArrayList; import java.util.List; // Driver Class class GFG { // main function public static void main(String[] args) { List<Integer> list1 = new ArrayList<Integer>(); list1.add(1); list1.add(2); list1.add(3); list1.add(4); Object[] array = list1.toArray(); System.out.print("The Array contains : "); for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } } OutputThe Array contains : 1 2 3 4 Overloaded toArray() MethodThis overloaded method of toArray() returns an array containing all elements inside the collection where the type of the returned array is what we specify inside the argument of the toArray() method. Syntax of Method<T> T[] toArray(T[] arr);Parameter: T denotes the type of element stored in the collectionReturn Type: The return type is what we specify inside the argument(i.e. T). Example of Overloaded toArray() MethodIn the example below, we have made some changes we need to understand before going further. String[] array=list1.toArray(new String[0]);This line is different from the above example. Here in this line, we have passed a string array as an argument to the function. Due to this, it returns us an array of string type (i.e. name array in this example ) having the same size and containing all the elements as of calling collection list1. Note: The major advantage of using the overloaded Overloaded toArray() method is it provides compile-time type safety as it returns an array of specific type only(e.g. Integer,String etc.) but the first syntax returns an array of Object type. Example of Overloaded toArray() MethodBelow is the implementation of the above method: Java import java.io.*; import java.util.ArrayList; import java.util.List; class GFG { public static void main(String[] args) { List<String> list1 = new ArrayList<String>(); list1.add("Pen"); list1.add("Paper"); list1.add("Rubber"); list1.add("Pencil"); String[] array = list1.toArray(new String[0]); System.out.println("The Array contains : "); for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } } OutputThe Array contains : Pen Paper Rubber Pencil Comment More infoAdvertise with us Next Article Java Collection toArray() Method S saswatdas121 Follow Improve Article Tags : Java Geeks Premier League Java-Collections Geeks Premier League 2023 Practice Tags : JavaJava-Collections Similar Reads AbstractCollection toArray() Method in Java 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 impo 5 min read Set toArray() Method in Java 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() me 2 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: T 2 min read AbstractCollection toString() Method in Java In Java, the toString() method is defined in the Object class and is inherited by all the Java classes. It is used to return a string representation of an object. The AbstractCollection class is a part of the Java Collections Framework, which overrides this method to provide a string representation 1 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 a 2 min read JavaTuple toArray() method The toArray() method in org.javatuples is used to convert the values in TupleClass into an array of Object type. This method is inherited from the JavaTuple class. This method can be used to any tuple class object of javatuples library. It returns an array of Object type formed with the values in th 2 min read LinkedList toArray() Method in Java In Java, the toArray() method is used to convert a LinkedList into an Array. It returns the same LinkedList elements but in the form of an Array only.Example: Here, we use toArray() to convert a LinkedList into an Array of Integer.Java// Java Program to Demonstrate how to use toArray() method // to 4 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 Conc 4 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 retur 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 return 2 min read Like