Skip to content
Related Articles

Related Articles

DelayQueue toArray() method in Java with Examples
  • Last Updated : 07 Jan, 2019

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 array T[] a as parameter which is the array into which the elements of the list 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 function returns an array containing all the elements in this list.

Exception: The first overload of this method throws no exceptions. However, the second overload throws following exceptions:



  • ArrayStoreException: if the runtime type of the specified array is not a supertype of the runtime type of every element in this queue.
  • NullPointerException: if the specified array is null.

Below programs illustrate the DelayQueue.toArray() method:

Program 1:




// Java Program Demonstrate DelayQueue toArray() method
  
import java.util.concurrent.*;
import java.util.*;
  
// The DelayObject for DelayQueue
// It must implement Delayed and
// its getDelay() and compareTo() method
class DelayObject implements Delayed {
  
    private String name;
    private long time;
  
    // Contructor of DelayObject
    public DelayObject(String name, long delayTime)
    {
        this.name = name;
        this.time = System.currentTimeMillis()
                    + delayTime;
    }
  
    // Implementing getDelay() method of Delayed
    @Override
    public long getDelay(TimeUnit unit)
    {
        long diff = time - System.currentTimeMillis();
        return unit.convert(diff, TimeUnit.MILLISECONDS);
    }
  
    // Implementing compareTo() method of Delayed
    @Override
    public int compareTo(Delayed obj)
    {
        if (this.time < ((DelayObject)obj).time) {
            return -1;
        }
        if (this.time > ((DelayObject)obj).time) {
            return 1;
        }
        return 0;
    }
  
    // Implementing toString() method of Delayed
    @Override
    public String toString()
    {
        return "\n{"
            + " " + name + ", time=" + time + "}";
    }
}
  
// Driver Class
public class GFG {
    public static void main(String[] args) throws InterruptedException
    {
  
        // create object of DelayQueue
        // using DelayQueue() constructor
        BlockingQueue<DelayObject> DQ
            = new DelayQueue<DelayObject>();
  
        // Add numbers to end of DelayQueue
        // using add() method
        DQ.add(new DelayObject("A", 1));
        DQ.add(new DelayObject("B", 2));
        DQ.add(new DelayObject("C", 3));
        DQ.add(new DelayObject("D", 4));
  
        // print queue
        System.out.println("DelayQueue: "
                           + DQ);
  
        // Get the array of the elements
        // of the ArrayList
        // using toArray() method
        Object[] arr = DQ.toArray();
  
        // print the array elements
        System.out.println("Elements of DelayQueue"
                           + " as Array: "
                           + Arrays.toString(arr));
    }
}
Output:
DelayQueue: [
{ A, time=1546842694862}, 
{ B, time=1546842694863}, 
{ C, time=1546842694864}, 
{ D, time=1546842694865}]
Elements of DelayQueue as Array: [
{ A, time=1546842694862}, 
{ B, time=1546842694863}, 
{ C, time=1546842694864}, 
{ D, time=1546842694865}]

Program 2:




// Java Program Demonstrate DelayQueue toArray() method
  
import java.util.concurrent.*;
import java.util.*;
  
// The DelayObject for DelayQueue
// It must implement Delayed and
// its getDelay() and compareTo() method
class DelayObject implements Delayed {
  
    private String name;
    private long time;
  
    // Contructor of DelayObject
    public DelayObject(String name, long delayTime)
    {
        this.name = name;
        this.time = System.currentTimeMillis()
                    + delayTime;
    }
  
    // Implementing getDelay() method of Delayed
    @Override
    public long getDelay(TimeUnit unit)
    {
        long diff = time - System.currentTimeMillis();
        return unit.convert(diff, TimeUnit.MILLISECONDS);
    }
  
    // Implementing compareTo() method of Delayed
    @Override
    public int compareTo(Delayed obj)
    {
        if (this.time < ((DelayObject)obj).time) {
            return -1;
        }
        if (this.time > ((DelayObject)obj).time) {
            return 1;
        }
        return 0;
    }
  
    // Implementing toString() method of Delayed
    @Override
    public String toString()
    {
        return "\n{"
            + " " + name + ", time=" + time + "}";
    }
}
  
// Driver Class
public class GFG {
    public static void main(String[] args) throws InterruptedException
    {
  
        // create object of DelayQueue
        // using DelayQueue() constructor
        BlockingQueue<DelayObject> DQ
            = new DelayQueue<DelayObject>();
  
        // Add numbers to end of DelayQueue
        // using add() method
        DQ.add(new DelayObject("A", 1));
        DQ.add(new DelayObject("B", 2));
        DQ.add(new DelayObject("C", 3));
        DQ.add(new DelayObject("D", 4));
  
        // print queue
        System.out.println("DelayQueue: "
                           + DQ);
  
        // Get the array of the elements
        // of the DelayQueue
        // using toArray(T[]) method
        Delayed arr[] = new Delayed[DQ.size()];
        arr = DQ.toArray(arr);
  
        // print the array elements
        System.out.println("Elements of ArrayList"
                           + " as Array: "
                           + Arrays.toString(arr));
    }
}
Output:
DelayQueue: [
{ A, time=1546842699503}, 
{ B, time=1546842699504}, 
{ C, time=1546842699505}, 
{ D, time=1546842699506}]
Elements of ArrayList as Array: [
{ A, time=1546842699503}, 
{ B, time=1546842699504}, 
{ C, time=1546842699505}, 
{ D, time=1546842699506}]

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :