AbstractQueue add() method in Java with examples Last Updated : 26 Nov, 2018 Comments Improve Suggest changes Like Article Like Report 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: Java // Java program to illustrate the // AbstractQueue add() method import java.util.*; import java.util.concurrent.LinkedBlockingQueue; public class GFG1 { public static void main(String[] argv) throws Exception { // Creating object of AbstractQueue<Integer> AbstractQueue<Integer> AQ = new LinkedBlockingQueue<Integer>(); // Populating AQ AQ.add(10); AQ.add(20); AQ.add(30); AQ.add(40); AQ.add(50); // print AQ System.out.println("AbstractQueue contains : " + AQ); } } Output: AbstractQueue contains : [10, 20, 30, 40, 50] Program 2: Program for IllegalStateException Java // Java program to illustrate the // AbstractQueue add() method // IllegalStateException import java.util.*; import java.util.concurrent.LinkedBlockingQueue; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Creating object of AbstractQueue<Integer> AbstractQueue<Integer> AQ = new LinkedBlockingQueue<Integer>(2); // Populating AQ AQ.add(10); AQ.add(20); AQ.add(30); AQ.add(40); AQ.add(50); // print AQ System.out.println("AbstractQueue contains : " + AQ); } catch (Exception e) { System.out.println("exception: " + e); } } } Output: exception: java.lang.IllegalStateException: Queue full Program 3: Program for NullPointerException Java // Java program to illustrate the // AbstractQueue add() method // NullPointerException import java.util.*; import java.util.concurrent.LinkedBlockingQueue; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Creating object of AbstractQueue<Integer> AbstractQueue<Integer> AQ = new LinkedBlockingQueue<Integer>(); // Populating AQ AQ.add(10); AQ.add(20); AQ.add(30); AQ.add(null); AQ.add(50); // print AQ System.out.println("AbstractQueue contains : " + AQ); } catch (Exception e) { System.out.println("exception: " + e); } } } Output: exception: java.lang.NullPointerException Reference: https://docs.oracle.com/javase/8/docs/api/java/util/AbstractQueue.html#add-E- Comment More infoAdvertise with us Next Article AbstractQueue add() method in Java with examples G gopaldave Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions java-AbstractQueue +1 More Practice Tags : JavaJava-Collections Similar Reads AbstractQueue addAll() method in Java with examples The addAll(E e) method of AbstractQueue adds all of the elements in the specified collection to this queue. Syntax: public boolean addAll(Collection c) Parameters: This method accepts a mandatory parameter collection containing elements to be added to this queue Returns: The method returns true if t 3 min read AbstractSet add() method in Java with Example The add(E) method of AbstractSet Class appends the specified element to the end of this AbstractSet. Syntax: boolean add(E element) Parameters: This function accepts a single parameter element as shown in the above syntax. The element specified by this parameter is appended to end of the AbstractSet 2 min read Abstract Method in Java with Examples In Java, Sometimes we require just method declaration in super-classes. This can be achieved by specifying the Java abstract type modifier. Abstraction can be achieved using abstract class and abstract methods. In this article, we will learn about Java Abstract Method. Java Abstract MethodThe abstra 6 min read AbstractCollection add() Method in Java with Examples The add() method in Java AbstractCollection is used to add a specific element into a Collection. This method will add the element only if the specified element is not present in the Collection else the function will return False if the element is already present in the Collection. Syntax: AbstractCo 2 min read AbstractList addAll() method in Java with Examples The addAll() method of java.util.AbstractList class is used to insert all of the elements in the specified collection into this list at the specified position. This shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elem 5 min read Like