LinkedBlockingQueue toArray() method in Java
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
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
{
int capacityOfQueue = 50 ;
LinkedBlockingQueue<Integer> linkedQueue
= new LinkedBlockingQueue<Integer>(capacityOfQueue);
linkedQueue.add( 2300 );
linkedQueue.add( 1322 );
linkedQueue.add( 8945 );
linkedQueue.add( 6512 );
System.out.println( "Queue Contains "
+ linkedQueue);
Object[] array = linkedQueue.toArray();
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
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
{
int capacityOfQueue = 5 ;
LinkedBlockingQueue<String> linkedQueue
= new LinkedBlockingQueue<String>(capacityOfQueue);
linkedQueue.offer( "User" );
linkedQueue.offer( "Employee" );
linkedQueue.offer( "Manager" );
linkedQueue.offer( "Analyst" );
linkedQueue.offer( "HR" );
Object[] array = linkedQueue.toArray();
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
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
{
int capacityOfQueue = 10 ;
LinkedBlockingQueue<String> linkedQueue
= new LinkedBlockingQueue<String>(capacityOfQueue);
linkedQueue.offer( "Sonali" );
linkedQueue.offer( "Sonam" );
linkedQueue.offer( "Kajal" );
linkedQueue.offer( "Komal" );
System.out.println( "Queue Contains : " + linkedQueue);
String[] passArray = new String[linkedQueue.size()];
Object[] array = linkedQueue.toArray(passArray);
System.out.println( "\nThe array passed :" );
for (Object i : passArray) {
System.out.print(i + "\t" );
}
System.out.println();
System.out.println( "\nThe array returned :" );
for (Object i : array) {
System.out.print(i + "\t" );
}
}
}
|
Output
Queue 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
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
int capacityOfQueue = 4 ;
LinkedBlockingQueue<Integer>
linkedQueue = new LinkedBlockingQueue<Integer>(capacityOfQueue);
linkedQueue.put( 46541 );
linkedQueue.put( 44648 );
linkedQueue.put( 45654 );
String[] arr = {};
try {
String[] array = linkedQueue.toArray(arr);
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
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
int capacityOfQueue = 4 ;
LinkedBlockingQueue<String>
linkedQueue = new LinkedBlockingQueue<String>(capacityOfQueue);
linkedQueue.put( "aman" );
linkedQueue.put( "khan" );
linkedQueue.put( "Prakash" );
String[] arr = null ;
try {
String[] array = linkedQueue.toArray(arr);
System.out.println( "Items in Queue are " + array);
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
}
}
|
Output:
Exception: java.lang.NullPointerException
Reference:
Last Updated :
14 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...