ArrayBlockingQueue toArray() Method in Java
- 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:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
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:// Program Demonstrate how to apply toArray() method// of ArrayBlockingQueue Class.importjava.util.concurrent.ArrayBlockingQueue;publicclassGFG {publicstaticvoidmain(String[] args){// Define capacity of ArrayBlockingQueueintcapacity =5;// Create object of ArrayBlockingQueueArrayBlockingQueue<Integer> queue =newArrayBlockingQueue<Integer>(capacity);// Add 5 elements to ArrayBlockingQueuequeue.offer(423);queue.offer(422);queue.offer(421);queue.offer(420);queue.offer(424);// Create a array by calling toArray() methodObject[] array = queue.toArray();// Print queueSystem.out.println("Queue is "+ queue);// Print elements of arraySystem.out.println("The array created by toArray() is:");for(Object i : array) {System.out.println(i);}}}Output:Queue is [423, 422, 421, 420, 424] The array created by toArray() is: 423 422 421 420 424
Program 2:
// Program Demonstrate how to apply toArray() method// of ArrayBlockingQueue Class.importjava.util.concurrent.ArrayBlockingQueue;publicclassGFG {publicstaticvoidmain(String[] args){// Define capacity of ArrayBlockingQueueintcapacity =5;// Create object of ArrayBlockingQueueArrayBlockingQueue<String> queue =newArrayBlockingQueue<String>(capacity);// Add 5 elements to ArrayBlockingQueuequeue.offer("User");queue.offer("Employee");queue.offer("Manager");queue.offer("Analyst");queue.offer("HR");// Create a array by calling toArray() methodObject[] array = queue.toArray();// Print queueSystem.out.println("Queue is "+ queue);// Print elements of arraySystem.out.println("The array created by toArray() is:");for(Object i : array) {System.out.println(i);}}}Output: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. ThenString[] new_arr = queue.toArray(new String[0]);
Note that toArray(new Object[0]) is same to 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.
Below programs illustrate toArray(T[] a) method of ArrayBlockingQueue class:
Program 1:// Program Demonstrate how to apply toArray(T[] a) method// of ArrayBlockingQueue Class.importjava.util.concurrent.ArrayBlockingQueue;publicclassGFG {publicstaticvoidmain(String[] args){// Define capacity of ArrayBlockingQueueintcapacity =5;// Create object of ArrayBlockingQueueArrayBlockingQueue<String> queue =newArrayBlockingQueue<String>(capacity);// Add 5 elements to ArrayBlockingQueuequeue.offer("User");queue.offer("Employee");queue.offer("Manager");queue.offer("Analyst");queue.offer("HR");// Creating the arrayString[] array =newString[5];// Calling toArray(T[] a) methodObject[] ReturnArray = queue.toArray(array);// Print queueSystem.out.println("Queue is "+ queue);// Print elements of array passed as parameterSystem.out.println();System.out.println("The array passed to toArray() is:");for(Object i : array) {System.out.println(i);}// Print elements of array retuned by method toArray()System.out.println();System.out.println("The array retuned by toArray() is:");for(Object i : ReturnArray) {System.out.println(i);}}}Output:Queue is [User, Employee, Manager, Analyst, HR] The array passed to toArray() is: User Employee Manager Analyst HR The array retuned by toArray() is: User Employee Manager Analyst HR
Reference:
- https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ArrayBlockingQueue.html#toArray
- https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ArrayBlockingQueue.html#toArray-T:A


