DelayQueue clear() method in Java
The clear() method of DelayQueue in Java is used to remove all of the elements in the current DelayQueue object. The queue will be empty after this call is returned. The elements in the queue whose Delay does not have an Expiry are automatically discarded from the Queue.
Syntax:
public void clear()
Parameters: This method does not takes any parameter.
Return Value: This method does not returns any value.
Below program illustrate the above method:
// Java program to illustrate the clear() 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; } }; // Add the obj to the queue queue.add(obj); // Check if queue is empty System.out.println("Is queue empty() : " + queue.isEmpty()); // Remove all elements from queue // uing clear() method queue.clear(); // Check if queue is empty System.out.println("Is queue empty after calling clear() : " + queue.isEmpty()); } } |
Is queue empty() : false Is queue empty after calling clear() : true
Recommended Posts:
- DelayQueue take() method in Java with Examples
- DelayQueue put() method in Java with Examples
- DelayQueue remove() method in Java
- DelayQueue add() method in Java with Examples
- DelayQueue iterator() method in Java with Examples
- DelayQueue poll() method in Java with Examples
- DelayQueue offer() method in Java with Examples
- DelayQueue peak() method in Java with Examples
- DelayQueue drainTo() method in Java with Examples
- DelayQueue size() method in Java with Examples
- DelayQueue toArray() method in Java with Examples
- DelayQueue remainingCapacity() method in Java with Examples
- Map clear() method in Java with Example
- ConcurrentLinkedDeque clear() method in Java
- ConcurrentSkipListSet clear() method in Java
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.



