DelayQueue clear() method in Java Last Updated : 14 Apr, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // 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 // using clear() method queue.clear(); // Check if queue is empty System.out.println("Is queue empty after calling clear() : " + queue.isEmpty()); } } Output:Is queue empty() : false Is queue empty after calling clear() : true Comment More infoAdvertise with us Next Article DelayQueue remove() method in Java P psil123 Follow Improve Article Tags : Java Java - util package java-DelayQueue Practice Tags : Java Similar Reads DelayQueue Class in Java In Java, the DelayQueue Class is the part of the java.util.concurrent package and implements the BlockingQueue interface. DelayQueue is a specialized implementation of a blocking queue that orders elements based on their delay time. Only elements whose delay has expired can be retrieved from the que 12 min read DelayQueue remove() method in Java The remove() method of DelayQueue class in Java is used to remove a single instance of the given object say obj from this DelayQueue if it is present. It returns true if the given element is removed successfully otherwise it returns false. Syntax: public boolean remove(Object obj) Parameters: This m 2 min read HashMap clear() Method in Java The clear() method of the HashMap class in Java is used to remove all of the elements or mappings (key-value pairs) from a specified HashMap.Example 2: Here, we will use the clear() method to clear a HashMap of Integer keys and String values. Java// Clearing HashMap of Integer keys // and String val 2 min read Hashtable clear() Method in Java The java.util.Hashtable.clear() method in Java is used to clear and remove all of the keys from a specified Hashtable. Syntax: Hash_table.clear() Parameters: The method does not accept any parameters. Return Value: The method does not return any value. Below programs are used to illustrate the worki 2 min read ArrayDeque clear() Method in Java The Java.util.ArrayDeque.clear() method in Java is used to remove all of the elements from the Deque. Using the clear() method only clears all the element from the deque and does not delete the deque. In other words, it can be said that the clear() method is used to only empty an existing ArrayDeque 2 min read IdentityHashMap clear() Method in Java The java.util.IdentityHashMap.clear() method in Java is used to clear and remove all of the elements or mappings from a specified map. Syntax: Identity_HashMap.clear() Parameters: The method does not accept any parameters. Return Value: The method does not return any value. Below programs are used t 2 min read Like