VOOZH about

URL: https://www.geeksforgeeks.org/java/abstractqueue-add-method-in-java-with-examples/

⇱ AbstractQueue add() method in Java with examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

AbstractQueue add() method in Java with examples

Last Updated : 11 Jul, 2025
The add(E e) method of AbstractQueue inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. It returns true upon success and throws an IllegalStateException if no space is currently available. Syntax:
public boolean add(E e)
Parameters: This method accepts a mandatory parameter e which is the element to be inserted to the queue. Returns: This method returns true if the element is inserted in the Queue else it returns false. Exception: This method throws following exceptions:
  • IllegalStateException: if the element cannot be added at this time due to capacity restrictions
  • NullPointerException: if the specified element is null
  • ClassCastException - if the class of the specified element prevents it from being added to this queue
  • IllegalArgumentException - if some property of this element prevents it from being added to this queue
Below programs illustrate add() method: Program 1:
Output:
AbstractQueue contains : [10, 20, 30, 40, 50]
Program 2: Program for IllegalStateException
Output:
exception: java.lang.IllegalStateException: Queue full
Program 3: Program for NullPointerException
Output:
exception: java.lang.NullPointerException
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/AbstractQueue.html#add-E-
Comment