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:
ConcurrentLinkedDeque push() method in Java with Examples
Next article icon

ConcurrentLinkedDeque toArray() method in Java with Example

Last Updated : 24 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
  • 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 the array. It acts as a bridge between array-based and collection-based APIs.

    Syntax

    public Object[] toArray()

    Parameters:It does not take in any parameter.

    Return Value:It returns an array containing all the elements in the deque.

    Below examples illustrates the ConcurrentLinkedDeque.toArray() method:

    Example 1:




    // Java Program Demonstrate toArray()
    // method of ConcurrentLinkedDeque
      
    import java.util.concurrent.*;
    import java.util.*;
      
    public class GFG {
        public static void main(String[] args)
            throws IllegalStateException
        {
      
            // create object of ConcurrentLinkedDeque
            ConcurrentLinkedDeque<Integer> deque
                = new ConcurrentLinkedDeque<Integer>();
      
            // Add numbers to end of ConcurrentLinkedDeque
            deque.add(7855642);
            deque.add(35658786);
            deque.add(5278367);
            deque.add(74381793);
      
            System.out.println("ConcurrentLinkedDeque: "
                               + deque);
      
            Object[] a = deque.toArray();
            System.out.println("Returned Array: "
                               + Arrays.toString(a));
        }
    }
     
     
    Output:
      ConcurrentLinkedDeque: [7855642, 35658786, 5278367, 74381793]  Returned Array: [7855642, 35658786, 5278367, 74381793]  
  • toArray(T[])

    The toArray(arr[]) method method of ConcurrentLinkedDeque class in Java is used to form an array of the same elements as that of the ConcurrentLinkedDeque. It returns an array containing all of the elements in this ConcurrentLinkedDeque in the correct order; the run-time type of the returned array is that of the specified array. If the ConcurrentLinkedDeque fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the run time type of the specified array and the size of this ConcurrentLinkedDeque.
    If the ConcurrentLinkedDeque fits in the specified array with room to spare (i.e., the array has more elements than the ConcurrentLinkedDeque), the element in the array immediately following the end of the ConcurrentLinkedDeque is deque to null. (This is useful in determining the length of the ConcurrentLinkedDeque only if the caller knows that the ConcurrentLinkedDeque does not contain any null elements.)

    Syntax:

    public <T> T[] toArray(T[] a)

    Parameters: The method accepts one parameter arr[] which is the array into which the elements of the ConcurrentLinkedDeque are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.

    Return Value: The method returns an array containing the elements similar to the ConcurrentLinkedDeque.

    Exception: The method might throw two types of exception:

    • ArrayStoreException: When the mentioned array is of the different type and is not able to compare with the elements mentioned in the ConcurrentLinkedDeque.
    • NullPointerException: If the array is Null, then this exception is thrown.

    Below program illustrates the working of the ConcurrentLinkedDeque.toArray(arr[]) method.

    Program 1: When array is of the size of ConcurrentLinkedDeque




    // Java code to illustrate toArray(arr[])
      
    import java.util.concurrent.*;
    import java.util.*;
      
    public class ConcurrentLinkedDequeDemo {
        public static void main(String args[])
        {
            // Creating an empty ConcurrentLinkedDeque
            ConcurrentLinkedDeque<String> deque
                = new ConcurrentLinkedDeque<String>();
      
            // Use add() method to add
            // elements into the ConcurrentLinkedDeque
            deque.add("Welcome");
            deque.add("To");
            deque.add("Geeks");
            deque.add("For");
            deque.add("Geeks");
      
            // Displaying the ConcurrentLinkedDeque
            System.out.println("The ConcurrentLinkedDeque: "
                               + deque);
      
            // Creating the array and using toArray()
            String[] arr = new String[5];
            arr = deque.toArray(arr);
      
            // Displaying arr
            System.out.println("Returned Array: "
                               + Arrays.toString(arr));
        }
    }
     
     
    Output:
      The ConcurrentLinkedDeque: [Welcome, To, Geeks, For, Geeks]  Returned Array: [Welcome, To, Geeks, For, Geeks]  


    Next Article
    ConcurrentLinkedDeque push() method in Java with Examples

    M

    MerlynShelley
    Improve
    Article Tags :
    • Java
    • Technical Scripter
    • Java - util package
    • Java-Collections
    • Java-ConcurrentLinkedDeque
    • Java-Functions
    • Technical Scripter 2018
    Practice Tags :
    • Java
    • Java-Collections

    Similar Reads

    • ConcurrentLinkedDeque pop() method in Java with Examples
      The Java.util.ConcurrentLinkedDeque.pop() method in Java is used to pop an element from the ConcurrentLinkedDeque. The element is popped from the top of the ConcurrentLinkedDeque and is removed from the same.Syntax: ConcurrentLinkedDeque.pop() Parameters: The method does not take any parameters.Retu
      2 min read
    • ConcurrentLinkedDeque peek() method in Java with Example
      The java.util.ConcurrentLinkedDeque.peek() method in Java is used to retrieve or fetch the element at the head of the Deque. The element retrieved does not get deleted or removed from the Deque instead the method just returns it. If no element is present in the deque then Null is returned. Syntax: A
      2 min read
    • ConcurrentLinkedDeque push() method in Java with Examples
      The push() method of ConcurrentLinkedDeque class is an in-built function in Java which pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success an
      2 min read
    • ConcurrentLinkedDeque addAll() method in Java with Examples
      The addAll(Collection col) of ConcurrentLinkedDeque which takes col as a parameter, where col is a Collection of elements (List, ArrayList, LinkedList etc). This entire Collection gets appended or added to the end of the Dequeue. This method just like add() method returns true if the Collection gets
      3 min read
    • ConcurrentLinkedQueue toArray() Method in Java
      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 Conc
      4 min read
    • ConcurrentLinkedDeque iterator() method in Java with Example
      The Java.util.concurrent.ConcurrentLinkedDeque.iterator() method is used to return an iterator of the same elements as that of the ConcurrentLinkedDeque. The elements are returned in random order from what was present in the deque. Syntax: Iterator iterate_value = ConcurrentLinkedDeque.iterator(); P
      2 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
    • DelayQueue toArray() method in Java with Examples
      The toArray() method of DelayQueue is used to return an array containing all the elements in DelayQueue. There elements are not in any specific order in the array.Syntax: public Object[] toArray () or public T[] toArray (T[] a) Parameters: This method either accepts no parameters or it takes an arra
      4 min read
    • ConcurrentLinkedDeque element() method in Java
      The java.util.concurrent.ConcurrentLinkedDeque.element() is an in-built function in java which retrieves but does not remove the head of the queue represented by deque i.e the first element of deque.Syntax: conn_linked_deque.element() Parameter: This method has no parameters.Return Value: This metho
      2 min read
    • ArrayList toArray() method in Java with Examples
      The toArray() method of ArrayList is used to return an array containing all the elements in ArrayList in the correct order. Declaring toArray() methodpublic Object[] toArray() or public <T> T[] toArray(T[] a)Parameters: This method either accepts no parameters or it takes an array T[] as a par
      3 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