LinkedBlockingDeque toArray() method in Java with Example Last Updated : 24 Dec, 2018 Comments Improve Suggest changes Like Article Like Report toArray() The Java.util.concurrent.LinkedBlockingDeque.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 LinkedBlockingDeque.toArray() method: Example 1: Java // Java Program Demonstrate toArray() // method of LinkedBlockingDeque import java.util.concurrent.*; import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of LinkedBlockingDeque LinkedBlockingDeque<Integer> LBD = new LinkedBlockingDeque<Integer>(); // Add numbers to end of LinkedBlockingDeque LBD.add(7855642); LBD.add(35658786); LBD.add(5278367); LBD.add(74381793); System.out.println("LinkedBlockingDeque: " + LBD); Object[] a = LBD.toArray(); System.out.println("Returned Array: " + Arrays.toString(a)); } } Output: LinkedBlockingDeque: [7855642, 35658786, 5278367, 74381793] Returned Array: [7855642, 35658786, 5278367, 74381793] toArray(T[]) The toArray(arr[]) method method of LinkedBlockingDeque class in Java is used to form an array of the same elements as that of the LinkedBlockingDeque. It returns an array containing all of the elements in this LinkedBlockingDeque in the correct order; the run-time type of the returned array is that of the specified array. If the LinkedBlockingDeque 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 LinkedBlockingDeque. If the LinkedBlockingDeque fits in the specified array with room to spare (i.e., the array has more elements than the LinkedBlockingDeque), the element in the array immediately following the end of the LinkedBlockingDeque is set to null. (This is useful in determining the length of the LinkedBlockingDeque only if the caller knows that the LinkedBlockingDeque 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 LinkedBlockingDeque 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 LinkedBlockingDeque. 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 LinkedBlockingDeque. NullPointerException: If the array is Null, then this exception is thrown. Below program illustrates the working of the LinkedBlockingDeque.toArray(arr[]) method. Program 1: When array is of the size of LinkedBlockingDeque Java // Java code to illustrate toArray(arr[]) import java.util.concurrent.*; import java.util.*; public class LinkedBlockingDequeDemo { public static void main(String args[]) { // Creating an empty LinkedBlockingDeque LinkedBlockingDeque<String> LBD = new LinkedBlockingDeque<String>(); // Use add() method to add // elements into the LinkedBlockingDeque LBD.add("Welcome"); LBD.add("To"); LBD.add("Geeks"); LBD.add("For"); LBD.add("Geeks"); // Displaying the LinkedBlockingDeque System.out.println("The LinkedBlockingDeque: " + LBD); // Creating the array and using toArray() String[] arr = new String[5]; arr = LBD.toArray(arr); // Displaying arr System.out.println("Returned Array: " + Arrays.toString(arr)); } } Output: The LinkedBlockingDeque: [Welcome, To, Geeks, For, Geeks] Returned Array: [Welcome, To, Geeks, For, Geeks] Comment More infoAdvertise with us Next Article LinkedBlockingDeque toArray() method in Java with Example P psil123 Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions Java-LinkedBlockingDeque +1 More Practice Tags : JavaJava-Collections Similar Reads LinkedBlockingDeque put() method in Java The put(E e) method of LinkedBlockingDeque inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque). If the Deque is capacity restricted, then it will wait for the space to become available. Syntax: public void put(E e) Parameters: This metho 2 min read LinkedBlockingDeque putFirst() method in Java The putFirst(E e) method of LinkedBlockingDeque inserts the specified element at the front of the queue represented by this deque. If the Deque is capacity restricted, then it will wait for the space to become available. Syntax: public void putFirst(E e) Parameters: This method accepts a mandatory p 2 min read LinkedBlockingDeque removeFirst() method in Java The removeFirst() method of LinkedBlockingDeque returns and removes the first element of the Deque container from it. The method throws an NoSuchElementException if the Deque container is empty. Syntax: public E removeFirst() Returns: This method returns the head of the Deque container, which is the 2 min read LinkedBlockingDeque remainingCapacity() method in Java The remainingCapacity() method of LinkedBlockingDeque returns the number of additional elements that this deque can ideally (in the absence of memory or resource constraints) accept without blocking. remainingCapacity() = final_size() - current_size() Syntax: public int remainingCapacity() Parameter 2 min read LinkedBlockingDeque size() method in Java The size() method of LinkedBlockingDeque returns the current size of the Deque container. On calling the function the number of elements in the Deque container is returned. If the container is capacity restricted, then also it returns the number of elements which are present in the container at the 2 min read LinkedBlockingDeque spliterator() method in Java The spliterator() method of LinkedBlockingDeque returns a Spliterator on the elements of LinkedBlockingDeque. The returned iterator is weakly consistent. Spliterator can be used with Streams in Java 8. Spliterator can traverse elements individually and in bulk too. Syntax: public Spliterator spliter 2 min read LinkedBlockingDeque removeFirstOccurrence() method in Java The removeFirstOccurrence() method of LinkedBlockingDeque removes the first occurrence of the specified element from this deque. If the deque does not contain the element, it remains unchanged. It returns true if this deque contained the specified element, else it returns false. Syntax: public boole 2 min read LinkedBlockingDeque takeLast() method in Java The takeLast() method of LinkedBlockingDeque returns and removes the tail of the Deque container from it. The method throws an InterruptedException if it is interrupted while waiting. Syntax: public E takeLast() Returns: This method returns the tail(last element) of the Deque container. Exception: T 2 min read LinkedBlockingDeque take() method in Java The take() method of LinkedBlockingDeque returns and removes the head of the Deque container from it. The method throws an InterruptedException if it is interrupted while waiting. Syntax: public E take() Returns: This method returns the head of the Deque container. Exception: The function throws a I 2 min read LinkedBlockingDeque removeLastOccurrence() method in Java The removeLastOccurrence() method of LinkedBlockingDeque removes the last occurrence of the specified element from this deque. If the deque does not contain the element, it remains unchanged. It returns true if this deque contained the specified element, else it returns false. Syntax: public boolean 2 min read Like