GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Queue add() method in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Queue add() method in Java

Last Updated : 11 Jul, 2025

The add(E e) method of Queue Interface inserts the element passed in the parameter to the end of the queue if there is space. If the Queue is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion. Syntax:

boolean add(E e)

Parameters: This method accepts a mandatory parameter e which is the element to be inserted in the end of the Queue. Returns: This method returns true on successful insertion. Exceptions: The function throws four exceptions which are described as below:

  • ClassCastException: when the class of the element to be entered prevents it from being added to this container.
  • IllegalStateException: when the capacity of the container is full and insertion is done.
  • IllegalArgumentException: when some property of the element prevents it to be added to the queue.
  • NullPointerException: when the element to be inserted is passed as null and the Queue's interface does not allow null elements.

Below programs illustrate add() method of Queue: Program 1: With the help of LinkedList. 

Java
// Java Program Demonstrate add()
// method of Queue

import java.util.*;

public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {

        // create object of Queue
        Queue<Integer> Q
            = new LinkedList<Integer>();

        // Add numbers to end of Queue
        Q.add(7855642);
        Q.add(35658786);
        Q.add(5278367);
        Q.add(74381793);

        // before removing print queue
        System.out.println("Queue: " + Q);
    }
}
Output:
Queue: [7855642, 35658786, 5278367, 74381793]

Program 2: With the help of ArrayDeque. 

Java
// Java Program Demonstrate add()
// method of Queue

import java.util.*;

public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {

        // create object of Queue
        Queue<Integer> Q
            = new ArrayDeque<Integer>();

        // Add numbers to end of Queue
        Q.add(7855642);
        Q.add(35658786);
        Q.add(5278367);
        Q.add(74381793);

        // before removing print queue
        System.out.println("Queue: " + Q);
    }
}
Output:
Queue: [7855642, 35658786, 5278367, 74381793]

Program 3: With the help of LinkedBlockingDeque. 

Java
// Java Program Demonstrate add()
// method of Queue

import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;

public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {

        // create object of Queue
        Queue<Integer> Q
            = new LinkedBlockingDeque<Integer>();

        // Add numbers to end of Queue
        Q.add(7855642);
        Q.add(35658786);
        Q.add(5278367);
        Q.add(74381793);

        // before removing print queue
        System.out.println("Queue: " + Q);
    }
}
Output:
Queue: [7855642, 35658786, 5278367, 74381793]

Program 4: With the help of ConcurrentLinkedDeque. 

Java
// Java Program Demonstrate add()
// method of Queue

import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;

public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {

        // create object of Queue
        Queue<Integer> Q
            = new ConcurrentLinkedDeque<Integer>();

        // Add numbers to end of Queue
        Q.add(7855642);
        Q.add(35658786);
        Q.add(5278367);
        Q.add(74381793);

        // before removing print queue
        System.out.println("Queue: " + Q);
    }
}
Output:
Queue: [7855642, 35658786, 5278367, 74381793]

Below programs illustrate exceptions thrown by this method: Program 5: To show NullPointerException. 

Java
// Java Program Demonstrate add()
// method of Queue when Null is passed

import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;

public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {

        // create object of Queue
        Queue<Integer> Q
            = new LinkedBlockingQueue<Integer>();

        // Add numbers to end of Queue
        Q.add(7855642);
        Q.add(35658786);
        Q.add(5278367);

        try {
            // when null is inserted
            Q.add(null);
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}
Output:
Exception: java.lang.NullPointerException

Program 6: To show IllegalStateException. 

Java
// Java Program Demonstrate add()
// method of Queue when capacity is full
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;

public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {

        // create object of Queue
        Queue<Integer> Q
            = new LinkedBlockingQueue<Integer>(3);

        // Add numbers to end of Queue
        Q.add(7855642);
        Q.add(35658786);
        Q.add(5278367);

        try {
            // when capacity is full
            Q.add(10);
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}
Output:
Exception: java.lang.IllegalStateException: Queue full

Time Complexity : O(1)

Note: The other two exceptions are internal and are caused depending on the compiler hence cannot be shown in the compiler. Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html#add-E-

Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.