Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Queue add() method in Java
Next article icon

Queue add() method in Java

Last Updated : 19 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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-


Next Article
Queue add() method in Java

G

gopaldave
Improve
Article Tags :
  • Misc
  • Java
  • Java-Collections
  • Java - util package
  • java-basics
  • Java-Functions
  • java-queue
Practice Tags :
  • Java
  • Java-Collections
  • Misc

Similar Reads

    PriorityQueue add() Method in Java
    The Java.util.PriorityQueue.add() method in Java is used to add a specific element into a PriorityQueue. This method internally just calls the Java.util.PriorityQueue.offer() method with the value passed to it. So, it exactly works like offer() method. Syntax: Priority_Queue.add(Object element) Para
    2 min read
    Queue poll() method in Java
    The poll() method of Queue Interface returns and removes the element at the front end of the container. It deletes the element in the container. The method does not throws an exception when the Queue is empty, it returns null instead. Syntax: E poll() Returns: This method returns the element at the
    3 min read
    Queue peek() method in Java
    The peek() method of Queue Interface returns the element at the front the container. It does not deletes the element in the container. This method returns the head of the queue. The method does not throws an exception when the Queue is empty, it returns null instead. Syntax: E peek() Returns: This m
    3 min read
    Queue element() method in Java
    The element() method of Queue Interface returns the element at the front the container. It does not deletes the element in the container. This method returns the head of the queue. This method differs from peek() only in that it throws an exception if this queue is empty. Syntax: E element() Returns
    3 min read
    Queue offer() method in Java
    The offer(E e) method of Queue Interface inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. This method is preferable to add() method since this method does not throws an exception when the capacity of the container is full s
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences