Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

ArrayBlockingQueue toArray() Method in Java

  • Last Updated : 26 Nov, 2018
  1. 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.
      
    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);
            }
        }
    }
    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.
      
    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);
            }
        }
    }
    Output:
    Queue is [User, Employee, Manager, Analyst, HR]
    The array created by toArray() is:
    User
    Employee
    Manager
    Analyst
    HR
    
  2. 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 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.
      
    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");
      
            // Creating the array
            String[] array = new String[5];
      
            // Calling toArray(T[] a) method
            Object[] ReturnArray = queue.toArray(array);
      
            // Print queue
            System.out.println("Queue is " + queue);
      
            // Print elements of array passed as parameter
            System.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:




My Personal Notes arrow_drop_up