LinkedBlockingQueue toArray() method in Java
Last Updated :
14 Sep, 2021
toArray()
The toArray method of LinkedBlockingQueue is used to create an array having the same elements as that of this LinkedBlockingQueue, in proper sequence. Actually, this method copies all the elements from the LinkedBlockingQueue to a new array. This method behaves as a bridge between array and LinkedBlockingQueue.
Syntax:
public Object[] toArray()
Return Value: This method returns an array that contains the elements of LinkedBlockingQueue.
Below programs illustrates toArray() method of LinkedBlockingQueue class:
Program 1:
Java
// Java Program Demonstrate toArray()
// method of LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 50;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<Integer> linkedQueue
= new LinkedBlockingQueue<Integer>(capacityOfQueue);
// Add element to LinkedBlockingQueue
linkedQueue.add(2300);
linkedQueue.add(1322);
linkedQueue.add(8945);
linkedQueue.add(6512);
// print linkedQueue details
System.out.println("Queue Contains "
+ linkedQueue);
// apply toArray() method on linkedQueue
Object[] array = linkedQueue.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
Program 2:
Java
// Java Program Demonstrate toArray()
// method of LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 5;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<String> linkedQueue
= new LinkedBlockingQueue<String>(capacityOfQueue);
// Add 5 elements to ArrayBlockingQueue
linkedQueue.offer("User");
linkedQueue.offer("Employee");
linkedQueue.offer("Manager");
linkedQueue.offer("Analyst");
linkedQueue.offer("HR");
// apply toArray() method on linkedQueue
Object[] array = linkedQueue.toArray();
// Print elements of array
System.out.println("The array contains:");
for (Object i : array) {
System.out.print(i + " ");
}
}
}
Output: The array contains:
User Employee Manager Analyst HR
toArray(T[] a)
The toArray(T[] a) method of LinkedBlockingQueue is used to return an array containing the same elements as that of this LinkedBlockingQueue, 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 LinkedBlockingQueue 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 the 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 an 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.
Return Value: This method returns an array containing all of the elements in this queue.
Exception This method throws the following exceptions:
- ArrayStoreException: When the passed array is of a different type from the type of elements of LinkedBlockingQueue.
- NullPointerException: If the passed array is Null.
Below programs illustrates toArray(T[] a) method of LinkedBlockingQueue class:
Program 1
Java
// Java program to demonstrate
// toArray(T[] a) method
// method of LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 10;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<String> linkedQueue
= new LinkedBlockingQueue<String>(capacityOfQueue);
// Add 5 elements to ArrayBlockingQueue
linkedQueue.offer("Sonali");
linkedQueue.offer("Sonam");
linkedQueue.offer("Kajal");
linkedQueue.offer("Komal");
// Print queue
System.out.println("Queue Contains : " + linkedQueue);
// the array to pass in toArray()
// array has size equal to size of linkedQueue
String[] passArray = new String[linkedQueue.size()];
// Calling toArray(T[] a) method
Object[] array = linkedQueue.toArray(passArray);
// Print elements of passed array
System.out.println("\nThe array passed :");
for (Object i : passArray) {
System.out.print(i + "\t");
}
System.out.println();
// Print elements of returned array
System.out.println("\nThe array returned :");
for (Object i : array) {
System.out.print(i + "\t");
}
}
}
OutputQueue Contains : [Sonali, Sonam, Kajal, Komal]
The array passed :
Sonali Sonam Kajal Komal
The array returned :
Sonali Sonam Kajal Komal
Program 2: Passing different type of array in toArray() from the type of element LinkedBlockingQueue contains. The toArray() method will hence throw exception ArrayStoreException.
Java
// Java Program Demonstrate
// toArray()
// method Exceptions.
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 4;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<Integer>
linkedQueue = new LinkedBlockingQueue<Integer>(capacityOfQueue);
// add elements to queue
linkedQueue.put(46541);
linkedQueue.put(44648);
linkedQueue.put(45654);
// create a array of Strings
String[] arr = {};
// apply toArray method and Pass array of String
// as parameter to toArray method
try {
// LinkedBlockingQueue has type Integer but we are passing
// String Type array so toArray method will throw
// Exception
String[] array = linkedQueue.toArray(arr);
// print elements of queue
System.out.println("Items in Queue are " + array);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Output: Exception: java.lang.ArrayStoreException: java.lang.Integer
Program 3: Passing null array in toArray(). Hence toArray() method will throw NullPointerException.
Java
// Java program to demonstrate
// toArray() method Exceptions.
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 4;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<String>
linkedQueue = new LinkedBlockingQueue<String>(capacityOfQueue);
// add elements to queue
linkedQueue.put("aman");
linkedQueue.put("khan");
linkedQueue.put("Prakash");
// create a array of Strings
String[] arr = null;
// apply toArray method and Pass array of String
// as parameter to toArray method
try {
/// we are passing array with null values
String[] array = linkedQueue.toArray(arr);
// print elements of queue
System.out.println("Items in Queue are " + array);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Output: Exception: java.lang.NullPointerException
Reference:
Similar Reads
LinkedBlockingQueue Class in Java
The LinkedBlockingQueue in Java is part of the java.util.concurrent package and implements the BlockingQueue interface. It provides a thread-safe, bounded, or unbounded queue used for managing tasks in a producer-consumer scenario. This queue can be used in multithreaded environments where one threa
8 min read
LinkedBlockingQueue clear() method in Java
The clear() method of LinkedBlockingQueue removes all of the elements from this queue. After applying this method the queue will become empty. Syntax: public void clear() Below programs illustrates clear() method of LinkedBlockingQueue class: Program 1: Java // Java Program Demonstrate clear() // me
2 min read
LinkedBlockingQueue iterator() method in Java
The iterator() method of LinkedBlockingQueue returns an iterator of the same elements, as this LinkedBlockingQueue, in a proper sequence. The elements returned from this method contains all the elements in order from first(head) to last(tail) of LinkedBlockingQueue. The returned iterator is weakly c
3 min read
LinkedBlockingQueue drainTo() method in Java
The drainTo(Collection col) method of LinkedBlockingQueue removes all available elements from this LinkedBlocking Queue and adds them to the given collection passed as a parameter. drainTo(Collection<? super E> col) The drainTo(Collection<? super E> col) method of LinkedBlockingQueue rem
7 min read
LinkedBlockingQueue | offer() Method in JAVA
There is two types of offer() method for LinkedBlockingQueue class : offer(E e, long timeout, TimeUnit unit) The offer(E e, long timeout, TimeUnit unit) method of LinkedBlockingQueue inserts the element passed as parameter to method at the tail of this LinkedBlockingQueue if queue is not full. It wi
6 min read
LinkedBlockingQueue peek() method in Java
The peek() method of LinkedBlockingQueue returns the head of the LinkedBlockingQueue. It retrieves the value of the head of LinkedBlockingQueue but does not remove it. If the LinkedBlockingQueue is empty then this method returns null. Syntax: public E peek() Return Value: This method returns the hea
3 min read
LinkedBlockingQueue poll() method in Java
There is two types of poll() method in LinkedBlockingQueue. poll() The poll() method of LinkedBlockingQueue returns the head of LinkedBlockingQueue by removing that element from queue. It can be said that this method retrieves and removes element from head of this LinkedBlockingQueue. If queue is em
6 min read
LinkedBlockingQueue put() method in Java with Examples
The put(E e) method of LinkedBlockingQueue inserts element passed as parameter to method at the tail of this LinkedBlockingQueue, if queue is not full. If the queue is full, then this method will wait for space to become available and after space is available, it inserts the element to LinkedBlockin
3 min read
LinkedBlockingQueue remainingCapacity() method in Java
The remainingCapacity() method of LinkedBlockingQueue returns the number of more elements that can be added to LinkedBlockingQueue without blocking. The Capacity returned arises three cases: If remaining Capacity is Zero, then no more elements can be added to the LinkedBlockingQueue.If remaining Cap
3 min read
LinkedBlockingQueue remove() method in Java
The remove(Object obj) method of LinkedBlockingQueue removes only one instance of the given Object, passed as parameter, from this LinkedBlockingQueue if it is present. It removes an element e such that obj.equals(e) and if this queue contains one or more instance of element e. This method returns t
4 min read