ArrayBlockingQueue toArray() Method in Java

  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:

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
            }
        }
    }

    chevron_right

    
    

    Output:

    Queue is [423, 422, 421, 420, 424]
    The array created by toArray() is:
    423
    422
    421
    420
    424
    

    Program 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
            }
        }
    }

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
            }
        }
    }

    chevron_right

    
    

    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

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.