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.
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:
// Java Program Demonstrate toArray()
// method of ConcurrentLinkedDeque
// create object of ConcurrentLinkedDeque
// Add numbers to end of ConcurrentLinkedDeque
Object a = deque.toArray();
"Returned Array: "
ConcurrentLinkedDeque: [7855642, 35658786, 5278367, 74381793] Returned Array: [7855642, 35658786, 5278367, 74381793]
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.)
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)
// Creating an empty ConcurrentLinkedDeque
// Use add() method to add
// elements into the ConcurrentLinkedDeque
// Displaying the ConcurrentLinkedDeque
"The ConcurrentLinkedDeque: "
// Creating the array and using toArray()
String arr =
arr = deque.toArray(arr);
// Displaying arr
"Returned Array: "
The ConcurrentLinkedDeque: [Welcome, To, Geeks, For, Geeks] Returned Array: [Welcome, To, Geeks, For, Geeks]My Personal Notes arrow_drop_up
- ConcurrentLinkedDeque add() method in Java
- ConcurrentLinkedDeque removeLastOccurrence() Method in Java
- ConcurrentLinkedDeque peekLast() method in Java
- ConcurrentLinkedDeque element() method in Java
- ConcurrentLinkedDeque pop() method in Java with Examples
- ConcurrentLinkedDeque poll() method in Java with Example
- ConcurrentLinkedDeque peek() method in Java with Example
- ConcurrentLinkedDeque removeLast() method in Java
- ConcurrentLinkedDeque pollLast() method in Java
- ConcurrentLinkedDeque removeFirst() method in Java
- ConcurrentLinkedDeque contains() method in Java with Examples
- ConcurrentLinkedDeque pollFirst() method in Java
- ConcurrentLinkedDeque peekFirst() method in Java
- ConcurrentLinkedDeque clear() method in Java
- ConcurrentLinkedDeque iterator() method in Java with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
- Which Java libraries are useful for competitive programming?
- Integer.MAX_VALUE and Integer.MIN_VALUE in Java with Examples
- How to create a COVID-19 Tracker Android App
- Role of SemiColon in various Programming Languages
- How to calculate log base 2 of an Integer in Java?
- How to iterate over a TreeMap in Java?
- How to find duplicate elements in a Stream in Java
- Program to calculate Electricity Bill
- How to set Precision for Double values in Java?
- How to validate an IP address using Regular Expressions in Java
- Remove first element from ArrayList in Java
- Removing last element from ArrayList in Java
- Monolithic vs Microservices architecture
- Program to print half Diamond star pattern
- Java program to print Even length words in a String
- How to iterate over a 2D list (list of lists) in Java
- Interesting and Cool Tricks in Java
- What is a Webcrawler and where is it used?
- Difference between Core Java and Advanced Java
- Sum of all Palindrome Numbers present in a Linked list
- How to pad a String in Java
- Difference between Increment and Decrement Operators
- Program to check if a String in Java contains only whitespaces
- Program to check if the String is Null in Java
- How to build a simple Calculator app using Android Studio?
- Minimum Spanning Tree using Priority Queue and Array List
- How to validate identifier using Regular Expression in Java
- How to implement our own Dynamic Array class in Java?
- 8 Must-Have Skills for Becoming an Android App Developer
- Atomic Variables in Java with Examples