Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
ConcurrentLinkedQueue remove() method in Java
Next article icon

ConcurrentLinkedQueue toArray() Method in Java

Last Updated : 26 Nov, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
  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  

Next Article
ConcurrentLinkedQueue remove() method in Java

A

AmanSingh2210
Improve
Article Tags :
  • Java
  • Java-Collections
  • Java - util package
  • java-basics
  • Java-Functions
  • Java-ConcurrentLinkedQueue
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

  • ConcurrentLinkedQueue offer() method in Java
    The offer() method of ConcurrentLinkedQueue is used to insert the element, passed as parameter, at the tail of this ConcurrentLinkedQueue. This method returns True if insertion is successful. ConcurrentLinkedQueue is unbounded, so this method offer() will never returns false. Syntax: public boolean
    2 min read
  • ConcurrentLinkedQueue poll() method in Java
    The poll() method of ConcurrentLinkedQueue is used to remove and return the head of this ConcurrentLinkedQueue. If the ConcurrentLinkedQueue is empty then this method will return null. Syntax: public E poll() Returns: This method remove and returns the head of this ConcurrentLinkedQueue or null if t
    2 min read
  • ConcurrentLinkedQueue spliterator() method in Java
    The spliterator() method of ConcurrentLinkedQueue is used to get a Spliterator of the same elements as ConcurrentLinkedQueue. Created Spliterator is weakly consistent. It can be used with Streams in Java 8. Also it can traverse elements individually and in bulk too. Spliterator is better way to trav
    2 min read
  • ConcurrentLinkedQueue size() method in Java
    The size() method of ConcurrentLinkedQueue is used to returns number of elements that this ConcurrentLinkedQueue contains. Syntax: public int size() Returns: This method number of elements in this ConcurrentLinkedQueue. Below programs illustrate size() method of ConcurrentLinkedQueue: Example 1: //
    2 min read
  • ConcurrentLinkedQueue remove() method in Java
    The remove(Object o) method of ConcurrentLinkedQueue is used to remove a single instance of the specified element from this ConcurrentLinkedQueue, if it is present. This method removes an element e such that o.equals(e) if this ConcurrentLinkedQueue contains one or more such elements. Remove() metho
    2 min read
  • ConcurrentLinkedQueue peek() method in Java
    The peek() method of ConcurrentLinkedQueue is used to return the head of the ConcurrentLinkedQueue. It retrieves but does not remove, the head of this ConcurrentLinkedQueue. If the ConcurrentLinkedQueue is empty then this method returns null. Syntax: public E peek() Returns: This method returns the
    2 min read
  • ConcurrentLinkedQueue add() method in Java
    The add() method of ConcurrentLinkedQueue is used to insert the element, passed as parameter to add() of ConcurrentLinkedQueue, at the tail of this ConcurrentLinkedQueue. This method returns True if insertion is successful. ConcurrentLinkedQueue is unbounded, so this method will never throw IllegalS
    2 min read
  • ConcurrentLinkedDeque toArray() method in Java with Example
    toArray() The Java.util.concurrent.ConcurrentLinkedDeque.toArray() method returns an array containing all the elements in the deque in proper sequence i.e. from first to last. The returned array will be safe as a new array is created (hence new memory is allocated). Thus the caller is free to modify
    3 min read
  • ConcurrentLinkedDeque offerLast() Method in Java
    The java.util.concurrent.ConcurrentLinkedDeque.offerLast() method is an inbuilt method in Java which inserts the specified element, passed as a parameter, to the end of the deque. Syntax: Conn_Linked_Deque.offerLast(Object elem) Parameters: The method accepts a parameter elem which species the eleme
    2 min read
  • ConcurrentLinkedDeque offerFirst() method in Java
    The java.util.concurrent.ConcurrentLinkedDeque.offerFirst() method is an inbuilt method in Java which inserts the specified element, passed as a parameter, in the front of the deque. Syntax: Conn_Linked_Deque.offerFirst(Object elem) Parameters: The method accepts a parameter elem which species the e
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences