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 // 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 // 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] Comment More infoAdvertise with us Next Article ConcurrentLinkedDeque toArray() method in Java with Example M MerlynShelley Follow Improve Article Tags : Java Technical Scripter Technical Scripter 2018 Java-Collections Java - util package Java-Functions Java-ConcurrentLinkedDeque +3 More Practice Tags : JavaJava-Collections Similar Reads 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 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 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 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 para 3 min read ConcurrentLinkedDeque descendingIterator() method in Java with Example The Java.util.concurrent.ConcurrentLinkedDeque.descendingIterator() method is used to return an iterator of the same elements as the ConcurrentLinkedDeque but in the reverse order. Syntax: Iterator iterate_value = Array_Deque.descendingIterator(); Parameters: The method does not take any parameter. 2 min read CopyOnWriteArraySet toArray() method in Java with Example toArray() The Java.util.concurrent.CopyOnWriteArraySet.toArray() method returns an array containing all the elements in the set 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 3 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 AbstractSequentialList toArray() method in Java with Example The toArray() method of Java AbstractSequentialList is used to form an array of the same elements as that of the AbstractSequentialList. Basically, it copies all the element from a AbstractSequentialList to a new array. Syntax: Object[] arr = AbstractSequentialList.toArray() Parameters: The method d 2 min read Like