A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/abstractqueue-add-method-in-java-with-examples/ below:

AbstractQueue add() method in Java with examples

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:

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-

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4