Home  >  Java 11

Java Collection.toArray Method

By Arvind Rai, November 02, 2020
On this page we will provide Java Collection.toArray method example. The toArray method returns an array containing all the elements of the collection. By default toArray method returns Object[]. We can specify type of array as T[] or IntFunction<T[]> as argument in toArray method to return. If the Collection gives the guarantee of order, the returned array will contain elements in same order.
Now find the examples.

1. toArray()

The toArray() returns an array containing all elements of the collection. If the Collection gives the guarantee of order, the returned object array will contain elements in same order. For example, if collection is ArrayList, the order will be maintained and if collection is HashSet the order will not be necessarily maintained.
Find the method declaration from Java doc.
Object[] toArray() 
The above method returns Object[].
Find the example.
ToArrayDemo.java
package com.concretepage;
import java.util.Collection;
import java.util.List;
public class ToArrayDemo {
  public static void main(String[] args) {
	System.out.println("--- Example 1 ---");
	Collection<String> collection1 = List.of("A", "B" , "C");
	Object[] objArr1 = collection1.toArray();
	for(Object e : objArr1) {
	  System.out.print(String.valueOf(e + " "));
	}

	System.out.println("\n--- Example 2 ---");
	Collection<Integer> collection2 = List.of(10, 20, 30);
	Object[] objArr2 = collection2.toArray();
	for(Object e : objArr2) {
	  System.out.print(e + " ");
	}	
  }
}
Output
--- Example 1 ---
A B C 
--- Example 2 ---
10 20 30 

2. toArray(T[] a)

Find the toArray method of a type to contain the collection.
<T> T[] toArray(T[] a) 
The above toArray method returns an array containing all the elements and the type of returned array is that of specified array.
1 If the size of specified array is equal or greater to the size of collection then same reference of specified array is returned. The element in the returned array immediately following the end of the collection is set to null.
2 If the size of specified array is less than the size of collection then new array is created and returned.

Find the examples to display above scenarios.
ToArrayWithArrayDemo.java
package com.concretepage;
import java.util.Collection;
import java.util.List;
public class ToArrayWithArrayDemo {
  public static void main(String[] args) {
	System.out.println("--- Example 1 ---");
	Collection<Integer> collection1 = List.of(11, 12, 13);
	Integer[] numArr1 = new Integer[5];
	Integer[] numArr2 = collection1.toArray(numArr1);
	if (numArr1 == numArr2) {
	  System.out.println(true);
	} else {
	  System.out.println(false);
	}
	for (int i = 0; i < 3; i++) {
	  System.out.print(numArr2[i] + " ");
	}
	System.out.println("\nLength of numArr2: " + numArr2.length);

	System.out.println("--- Example 2 ---");
	Collection<String> collection2 = List.of("A", "B", "C");
	String[] strArr1 = new String[2];
	String[] strArr2 = collection2.toArray(strArr1);
	if (strArr1 == strArr2) {
	  System.out.println(true);
	} else {
	  System.out.println(false);
	}
	for (String s : strArr2) {
	  System.out.print(s + " ");
	}
	System.out.println("\nLength of strArr2: " + strArr2.length);

	System.out.println("--- Example 3 ---");
	Collection<String> collection3 = List.of("X", "Y", "Z");
	String[] arr1 = { "A", "B", "C", "D", "E" };
	String[] arr2 = collection3.toArray(arr1);
	for (String s : arr2) {
	  System.out.print(s + " ");
	}
	System.out.println("\nLength of arr2: " + arr2.length);
  }
} 
Output
--- Example 1 ---
true
11 12 13 
Length of numArr2: 5
--- Example 2 ---
false
A B C 
Length of strArr2: 3
--- Example 3 ---
X Y Z null E 
Length of arr2: 5 

3. toArray(IntFunction<T[]> generator)

In Java 11, the Collection has introduced a default method as toArray that accepts IntFunction as generator.
default <T> T[] toArray(IntFunction<T[]> generator) 
The above method returns an array containing all of elements in this collection using the provided generator function.
Find the example.
ToArrayIntFunctionDemo.java
package com.concretepage;
import java.util.Arrays;
import java.util.Collection;
import java.util.function.IntFunction;
public class ToArrayIntFunctionDemo {
  public static void main(String[] args) {
   System.out.println("--- Example 1 ---");
   Collection<Integer> collection1 = Arrays.asList(25, 35, 45);
   IntFunction<Integer[]> generator1 = Integer[]::new;
   Integer[] arr = collection1.toArray(generator1);
   for (int e : arr) {
	 System.out.print(e + " ");
   }
   
   System.out.println("\n--- Example 2 ---");	
   Collection<String> collection2 = Arrays.asList("A", "B", "C");
   IntFunction<String[]> generator2 = String[]::new;
   String[] str = collection2.toArray(generator2);
   for (String e : str) {
	 System.out.print(e + " ");
   }   
  }
}
Output
--- Example 1 ---
25 35 45 
--- Example 2 ---
A B C 

Reference

Interface Collection
POSTED BY
ARVIND RAI
ARVIND RAI








©2020 concretepage.com | Privacy Policy | Contact Us