GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — ConcurrentLinkedQueue toArray() Method in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

ConcurrentLinkedQueue toArray() Method in Java

Last Updated : 26 Nov, 2018
  1. toArray() : The toArray() method of ConcurrentLinkedQueue is used to returns an array of the same elements as that of the ConcurrentLinkedQueue in proper sequence. Basically, it copies all the element from a ConcurrentLinkedQueue to a new array. This method behaves as a bridge between array and ConcurrentLinkedQueue. Syntax:
    public Object[] toArray()
    Returns: The method returns an array containing the elements similar to the ConcurrentLinkedQueue. Below programs illustrate the java.util.concurrent.ConcurrentLinkedQueue.toArray() method. Example 1: Java
    // Java Program Demonstrate toArray()
    // method of ConcurrentLinkedQueue
    
    import java.util.concurrent.ConcurrentLinkedQueue;
    
    public class GFG {
    
        public static void main(String[] args)
        {
            // create object of ConcurrentLinkedQueue
            ConcurrentLinkedQueue<Integer> queue
                = new ConcurrentLinkedQueue<Integer>();
    
            // Add element to ConcurrentLinkedQueue
            queue.add(2300);
            queue.add(1322);
            queue.add(8945);
            queue.add(6512);
    
            // print queue details
            System.out.println("Queue Contains " + queue);
    
            // apply toArray() method on queue
            Object[] array = queue.toArray();
    
            // Print elements of array
            System.out.println("The array contains:");
            for (Object i : array) {
                System.out.print(i + "\t");
            }
        }
    }
    
    Output:
    Queue Contains [2300, 1322, 8945, 6512]
    The array contains:
    2300    1322    8945    6512
    
    Example 2: Java
    // Java Program Demonstrate toArray()
    // method of ConcurrentLinkedQueue
    
    import java.util.concurrent.ConcurrentLinkedQueue;
    
    public class GFG {
        public static void main(String args[])
        {
            // Creating a ConcurrentLinkedQueue
            ConcurrentLinkedQueue<String>
                queue = new ConcurrentLinkedQueue<String>();
    
            // elements into the Queue
            queue.add("Welcome");
            queue.add("To");
            queue.add("Jungle");
    
            // Displaying the ConcurrentLinkedQueue
            System.out.println("The ConcurrentLinkedQueue: "
                               + queue);
    
            // Creating the array and using toArray()
            Object[] arr = queue.toArray();
    
            System.out.println("The array is:");
            for (int j = 0; j < arr.length; j++)
                System.out.println(arr[j]);
        }
    }
    
    Output:
    The ConcurrentLinkedQueue: [Welcome, To, Jungle]
    The array is:
    Welcome
    To
    Jungle
    
  2. toArray(T[] a) : The toArray(T[] a) method of ConcurrentLinkedQueue is used to an array containing the same elements as that of this ConcurrentLinkedQueue in proper sequence. This method differs from toArray() in only one condition. The type of the returned array is the same as the passed array in the parameter, if the ConcurrentLinkedQueue size is less than or equal to the passed 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. Syntax:
    public <T> T[] toArray(T[] a)
    Parameter: This method takes array as parameter 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. Returns: This method returns array containing the elements similar to the ConcurrentLinkedQueue. Exception: This method throws following exceptions:
    • ArrayStoreException: When the passed array is of the different type from the type of elements of ConcurrentLinkedQueue.
    • NullPointerException: If the passed array is Null.
    Below programs illustrate the java.util.concurrent.ConcurrentLinkedQueue.toArray(T[] a) method. Example 1: Java
    // Java Program Demonstrate toArray()
    // method of ConcurrentLinkedQueue
    
    import java.util.concurrent.ConcurrentLinkedQueue;
    
    public class GFG {
    
        public static void main(String[] args)
        {
            // create object of ConcurrentLinkedQueue
            ConcurrentLinkedQueue<String> queue
                = new ConcurrentLinkedQueue<String>();
    
            // elements into the Queue
            queue.add("Welcome");
            queue.add("To");
            queue.add("Jungle");
    
            // print queue details
            System.out.println("Queue Contains " + queue);
    
            // the array to pass in toArray()
            // array has size equal to size of ConcurrentLinkedQueue
            String[] passArray = new String[queue.size()];
    
            // Calling toArray(T[] a) method
            Object[] array = queue.toArray(passArray);
    
            // Print elements of passed array
            System.out.println("\nThe array passed :");
            for (String i : passArray) {
                System.out.print(i + " ");
            }
    
            System.out.println();
    
            // Print elements of returned array
            System.out.println("\nThe array returned :");
            for (Object i : array) {
                System.out.print(i + " ");
            }
        }
    }
    
    Output:
    Queue Contains [Welcome, To, Jungle]
    
    The array passed :
    Welcome To Jungle 
    
    The array returned :
    Welcome To Jungle
    
    Example 2: To show ArrayStoreException Java
    // Java Program Demonstrate toArray()
    // method of ConcurrentLinkedQueue
    
    import java.util.concurrent.ConcurrentLinkedQueue;
    
    public class GFG {
    
        public static void main(String[] args)
        {
            // create object of ConcurrentLinkedQueue
            ConcurrentLinkedQueue<Integer> queue
                = new ConcurrentLinkedQueue<Integer>();
    
            // Add element to ConcurrentLinkedQueue
            queue.add(2323);
            queue.add(2472);
            queue.add(4235);
            queue.add(1242);
    
            // the array to pass in toArray()
            // array has size equal to size of ConcurrentLinkedQueue
            String[] passArray = new String[queue.size()];
    
            // Calling toArray(T[] a) method
            try {
                Object[] array = queue.toArray(passArray);
            }
            catch (ArrayStoreException e) {
                System.out.println("Exception: " + e);
            }
        }
    }
    
    Output:
    Exception: java.lang.ArrayStoreException: java.lang.Integer
    
    Example 2: To show NullPointerException Java
    // Java Program Demonstrate toArray()
    // method of ConcurrentLinkedQueue
    
    import java.util.concurrent.ConcurrentLinkedQueue;
    
    public class GFG {
    
        public static void main(String[] args)
        {
            // create object of ConcurrentLinkedQueue
            ConcurrentLinkedQueue<Integer> queue
                = new ConcurrentLinkedQueue<Integer>();
    
            // Add element to ConcurrentLinkedQueue
            queue.add(2323);
            queue.add(2472);
            queue.add(4235);
            queue.add(1242);
    
            // the array to pass
            String[] passArray = null;
    
            // Calling toArray(T[] a) method
            try {
                Object[] array = queue.toArray(passArray);
            }
            catch (NullPointerException e) {
                System.out.println("Exception: " + e);
            }
        }
    }
    
Output:
Exception: java.lang.NullPointerException
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.