Home » Java ConcurrentLinkedQueue toArray() Method

Java ConcurrentLinkedQueue toArray() Method

by Online Tutorials Library

Java ConcurrentLinkedQueue toArray() Method

The toArray() method of ConcurrentLinkedQueue class returns an array containing all the elements present in the queue in no particular order.

Syntax:

Parameters:

  1. NA
  2. a ? this is the array in which the elements of the queue will be stored.

Type Parameters:

T ? this is the component type of the array to hold the collection.

Specified By:

The toArray() method of ConcurrentLinkedQueue class is specified by :

toArray() method in interface Collection<E>.

Override:

The toArray() method of ConcurrentLinkedQueue class is overridden by :

toArray() method in class AbstractCollection<E>.

Return Value:

The toArray() method returns an array that contains all the elements of this queue.

Throws:

The toArray() method throws NullPointerException if the specified array is null.

Example 1

Test it Now

Output:

Element  1: Reema  Element  2: Rahul  Element  3: Rita  Element  4: Ramesh  

Example 2

Test it Now

Output:

Error:(22, 25) java: bad operand types for binary operator '>'    first type:  java.lang.Object    second type: java.lang.Object  

Operator ‘<‘ cannot be applied to java.lang.Object. If we do so, it will give you an error.

This example is the solution to the above described program.

Example 3

Test it Now

Output:

Elements : 8 5 17 80 2   Ascending order : 2 5 8 17 80   Decending order : 80 17 8 5 2  

Example 4

Test it Now

Output:

Exception in thread "main" java.lang.NullPointerException  at java.util.concurrent.ConcurrentLinkedQueue.toArray(ConcurrentLinkedQueue.java:638)  at com.tutorAspire.ConcurrentLinkedQueueToArrayExample4.main(ConcurrentLinkedQueueToArrayExample4.java:11)  

You may also like