DelayQueue size() method in Java with Examples

The size() method of DelayQueue class in Java is used to return the number of elements present in the given queue. If the number of elements is greater than Integer.MAX_VALUE then Integer.MAX_VALUE is returned as the size of this DelayQueue.

Syntax:

public int size()

Parameters: It does not takes in any parameter.

Return Value: It returns an integer which is the length of the queue.

Below program illustrate the size() method of DelayQueue in Java:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illutrate the size() method
// of DelayQueue class
  
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
  
public class GFG {
    public static void main(String args[])
    {
        // Create a DelayQueue Instance
        DelayQueue<Delayed> queue = new DelayQueue<Delayed>();
  
        // Create an object of type Delayed
        Delayed obj = new Delayed() {
  
            public long getDelay(TimeUnit unit)
            {
                return 24; // some value is returned
            }
  
            public int compareTo(Delayed o)
            {
                if (o.getDelay(TimeUnit.DAYS) > 
                           this.getDelay(TimeUnit.DAYS))
                    return 1;
                else if (o.getDelay(TimeUnit.DAYS) == 
                           this.getDelay(TimeUnit.DAYS))
                    return 0;
                return -1;
            }
        };
  
        // Insert the object to this DelayQueue
        // Size of the queue after insertion will be 1
        queue.add(obj);
  
        // print the size of this DelayQueue using
        // size() method
        System.out.println("Size : " + queue.size());
    }
}

chevron_right


Output:

Size : 1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.