The toArray() method of ArrayBlockingQueue class: The toArray() method of ArrayBlockingQueue class is used to create an array containing the same elements as that of this ArrayBlockingQueue, in proper sequence. Basically, it copies all the element from the ArrayBlockingQueue to a new array. This method behaves as a bridge between array and collections. Syntax:
public Object[] toArray()
Return Value: The method returns an array containing all of the elements in this queue. Below programs illustrate toArray() method of ArrayBlockingQueue class: Program 1:
Java
// Program Demonstrate how to apply toArray() method // of ArrayBlockingQueue Class. import java.util.concurrent.ArrayBlockingQueue; public class GFG { public static void main(String[] args) { // Define capacity of ArrayBlockingQueue int capacity = 5 ; // Create object of ArrayBlockingQueue ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity); // Add 5 elements to ArrayBlockingQueue queue.offer( 423 ); queue.offer( 422 ); queue.offer( 421 ); queue.offer( 420 ); queue.offer( 424 ); // Create a array by calling toArray() method Object[] array = queue.toArray(); // Print queue System.out.println("Queue is " + queue); // Print elements of array System.out.println("The array created by toArray() is:"); for (Object i : array) { System.out.println(i); } } } |
Queue is [423, 422, 421, 420, 424] The array created by toArray() is: 423 422 421 420 424
Program 2:
Java
// Program Demonstrate how to apply toArray() method // of ArrayBlockingQueue Class. import java.util.concurrent.ArrayBlockingQueue; public class GFG { public static void main(String[] args) { // Define capacity of ArrayBlockingQueue int capacity = 5 ; // Create object of ArrayBlockingQueue ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(capacity); // Add 5 elements to ArrayBlockingQueue queue.offer("User"); queue.offer("Employee"); queue.offer("Manager"); queue.offer("Analyst"); queue.offer("HR"); // Create a array by calling toArray() method Object[] array = queue.toArray(); // Print queue System.out.println("Queue is " + queue); // Print elements of array System.out.println("The array created by toArray() is:"); for (Object i : array) { System.out.println(i); } } } |
Queue is [User, Employee, Manager, Analyst, HR] The array created by toArray() is: User Employee Manager Analyst HR
The toArray(T[] arr) method of ArrayBlockingQueue class: The toArray(T[] a) method of ArrayBlockingQueue class is used to create an array containing the same elements as that of this ArrayBlockingQueue, in proper sequence. It has an additional condition. The type of the returned array is the same as the specified array in the parameter if the queue size is less than or equal to the specified array. Otherwise, a new array is allocated with the type same as the specified array and size of the array is equal to the size of this queue. This method behaves as a bridge between array and collections. Suppose a queue is an ArrayBlockingQueue and it contains only strings. Then
String[] new_arr = queue.toArray(new String[0]);
Note that toArray(new Object[0]) is same toArray() Method. Syntax:
public T[] toArray(T[] a)
Parameters: The method takes one parameter the arr which is an array into which all of the elements of the queue are to be copied, if it is big enough; otherwise, a new array of the same runtime type is allocated to this. Return Value: The method returns an array containing all of the elements in this queue. Exception: The method can throw one of the following exceptions:
- ArrayStoreException: When the passed array is of the different type and is not able to compare with the elements of the queue.
- NullPointerException: If the array is Null.
Reference: