Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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:
ConcurrentLinkedQueue addAll() method in Java
Next article icon

ConcurrentLinkedQueue add() method in Java

Last Updated : 26 Nov, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
The add() method of ConcurrentLinkedQueue is used to insert the element, passed as parameter to add() of ConcurrentLinkedQueue, at the tail of this ConcurrentLinkedQueue. This method returns True if insertion is successful. ConcurrentLinkedQueue is unbounded, so this method will never throw IllegalStateException or return false. Syntax:
public boolean add(E e)
Parameter: This method takes a single parameter e which represents the element to be insert into this ConcurrentLinkedQueue. Returns: This method returns true after successful insertion of element. Exception: This method throws NullPointerException if the specified element is null. Below programs illustrate add() method of ConcurrentLinkedQueue: Example 1: To demonstrate add() method of ConcurrentLinkedQueue to add String. Java
// Java Program Demonstrate add() // method of ConcurrentLinkedQueue  import java.util.concurrent.*;  public class GFG {     public static void main(String[] args)     {          // create an ConcurrentLinkedQueue         ConcurrentLinkedQueue<String>             queue = new ConcurrentLinkedQueue<String>();          // Add String to queue         queue.add("Kolkata");         queue.add("Patna");         queue.add("Delhi");         queue.add("Jammu");          // Displaying the existing ConcurrentLinkedQueue         System.out.println("ConcurrentLinkedQueue: " + queue);     } } 
Output:
  ConcurrentLinkedQueue: [Kolkata, Patna, Delhi, Jammu]  
Example 2: To demonstrate add() method of ConcurrentLinkedQueue for adding Numbers. Java
// Java Program Demonstrate add() // method of ConcurrentLinkedQueue  import java.util.concurrent.*;  public class GFG {     public static void main(String[] args)     {          // create an ConcurrentLinkedQueue         ConcurrentLinkedQueue<Integer>             queue = new ConcurrentLinkedQueue<Integer>();          // Add Numbers to queue         queue.add(4353);         queue.add(7824);         queue.add(78249);         queue.add(8724);          // Displaying the existing ConcurrentLinkedQueue         System.out.println("ConcurrentLinkedQueue: " + queue);     } } 
Output:
  ConcurrentLinkedQueue: [4353, 7824, 78249, 8724]  
Example 3: To demonstrate NullPointerException thrown by add() method for adding Null. Java
// Java Program Demonstrate add() // method of ConcurrentLinkedQueue  import java.util.concurrent.*;  public class GFG {     public static void main(String[] args)     {          // create an ConcurrentLinkedQueue         ConcurrentLinkedQueue<Integer>             queue = new ConcurrentLinkedQueue<Integer>();          // Add null to queue         try {             queue.add(null);         }         catch (NullPointerException e) {             System.out.println("Exception thrown "                                + "while adding null: " + e);         }     } } 
Output:
  Exception thrown while adding null: java.lang.NullPointerException  
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html#add-E-

Next Article
ConcurrentLinkedQueue addAll() method in Java

A

AmanSingh2210
Improve
Article Tags :
  • Java
  • Java-Collections
  • Java - util package
  • java-basics
  • Java-Functions
  • java-queue
  • Java-ConcurrentLinkedQueue
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

  • ConcurrentLinkedQueue addAll() method in Java
    The addAll() method of ConcurrentLinkedQueue is used to inserts all the elements of the Collection, passed as parameter to this method, at the end of a ConcurrentLinkedQueue. The insertion of element is in same order as returned by the collections iterator. Syntax: public boolean addAll(Collection
    4 min read
  • ConcurrentLinkedDeque add() method in Java
    The java.util.concurrent.ConcurrentLinkedDeque.add() is an in-built function in Java which inserts the specified element at the end of the deque. Syntax: conn_linked_deque.add(elem) Parameter: The method accepts only a single parameter elem which is to be added to tail of the ConcurentLinkedDeque. R
    2 min read
  • ConcurrentLinkedDeque addLast() method in Java
    The java.util.concurrent.ConcurrentLinkedDeque.addLast() is an in-built function in Java which inserts the specified element to the end of the deque. Syntax: conn_linked_deque.addLast(elem) Parameter: The method accepts only a single parameter elem which is to be added to the end of the ConcurrentLi
    2 min read
  • ConcurrentLinkedDeque addFirst() method in Java
    The java.util.concurrent.ConcurrentLinkedDeque.addFirst() is an in-built function in Java which inserts the specified element at the front of the ConcurrentLinkedDeque. Syntax: conn_linked_deque.addFirst(elem) Parameter: The method accepts only a single parameter elem which is to be added to the beg
    2 min read
  • ConcurrentLinkedQueue size() method in Java
    The size() method of ConcurrentLinkedQueue is used to returns number of elements that this ConcurrentLinkedQueue contains. Syntax: public int size() Returns: This method number of elements in this ConcurrentLinkedQueue. Below programs illustrate size() method of ConcurrentLinkedQueue: Example 1: //
    2 min read
  • ConcurrentLinkedQueue poll() method in Java
    The poll() method of ConcurrentLinkedQueue is used to remove and return the head of this ConcurrentLinkedQueue. If the ConcurrentLinkedQueue is empty then this method will return null. Syntax: public E poll() Returns: This method remove and returns the head of this ConcurrentLinkedQueue or null if t
    2 min read
  • ConcurrentLinkedQueue peek() method in Java
    The peek() method of ConcurrentLinkedQueue is used to return the head of the ConcurrentLinkedQueue. It retrieves but does not remove, the head of this ConcurrentLinkedQueue. If the ConcurrentLinkedQueue is empty then this method returns null. Syntax: public E peek() Returns: This method returns the
    2 min read
  • ConcurrentLinkedQueue offer() method in Java
    The offer() method of ConcurrentLinkedQueue is used to insert the element, passed as parameter, at the tail of this ConcurrentLinkedQueue. This method returns True if insertion is successful. ConcurrentLinkedQueue is unbounded, so this method offer() will never returns false. Syntax: public boolean
    2 min read
  • ConcurrentLinkedDeque clear() method in Java
    The java.util.concurrent.ConcurrentLinkedDeque.clear() method is an inbuilt method in Java which removes the elements in the Deque. Syntax: public void clear() Parameters: The method do not accepts any parameter. Return Value: The function does not return anything Below programs illustrate the Concu
    2 min read
  • ConcurrentLinkedQueue remove() method in Java
    The remove(Object o) method of ConcurrentLinkedQueue is used to remove a single instance of the specified element from this ConcurrentLinkedQueue, if it is present. This method removes an element e such that o.equals(e) if this ConcurrentLinkedQueue contains one or more such elements. Remove() metho
    2 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