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.
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:
AbstractQueue contains : [10, 20, 30, 40, 50]
Program 2: Program for IllegalStateException
exception: java.lang.IllegalStateException: Queue full
Program 3: Program for NullPointerException
- AbstractQueue addAll() method in Java with examples
- AbstractQueue remove() method in Java with examples
- AbstractQueue element() method in Java with examples
- AbstractQueue clear() method in Java with examples
- AbstractQueue in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.