DelayQueue add() method in Java with Examples Last Updated : 14 Jan, 2022 Comments Improve Suggest changes Like Article Like Report The add(E ele) method of DelayQueue class in Java is used to insert the given element into the delay queue and returns true if the element has been successfully inserted. Here, E refers to the type of elements maintained by this DelayQueue collection.Syntax: public boolean add(E ele) Parameters: This method takes only one parameter ele. It refers to the element which will be inserted into the delay queue.Return Value: It returns a boolean value which is true if the element has been added successfully otherwise it returns false.Exception: NullPointerException: This method throws a NullPointerException if a NULL is tried to inserted in this DelayQueue. Below programs illustrate the add() method of DelayQueue class:Program 1: Java // Java program to illustrate the add() // method in Java 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 instance of 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; } }; // Use the add() method to add obj to // the empty DelayQueue instance queue.add(obj); System.out.println("Size of the queue : " + queue.size()); } } Output: Size of the queue : 1 Program 2: Program to demonstrate the NullPointerException. Java // Java program to illustrate the Exception // thrown by add() 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 an instance of DelayQueue DelayQueue<Delayed> queue = new DelayQueue<Delayed>(); // Try to add NULL to the queue try { queue.add(null); } // Catch Exception catch (Exception e) { // Print Exception raised System.out.println(e); } } } Output: java.lang.NullPointerException Create Quiz Comment P psil123 Follow 0 Improve P psil123 Follow 0 Improve Article Tags : Java Java - util package java-DelayQueue Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like