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 poll() method in Java
Next article icon

Queue poll() method in Java

Last Updated : 20 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report
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 front of the container or the head of the Queue. It returns null when the Queue is empty. Below programs illustrate poll() method of Queue: Program 1: With the help of LinkedList. Java
// Java Program Demonstrate poll() // 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);          // print queue         System.out.println("Queue: " + Q);          // print head and deletes the head         System.out.println("Queue's head: " + Q.poll());          // print head and deleted the head         System.out.println("Queue's head: " + Q.poll());     } } 
Output:
  Queue: [7855642, 35658786, 5278367, 74381793]  Queue's head: 7855642  Queue's head: 35658786  
Program 2: To Demonstrate poll() method of Queue when the Queue becomes empty Java
// Java Program Demonstrate poll() // method of Queue when the Queue becomes empty  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(423);         Q.add(3432);          // print queue         System.out.println("Queue: " + Q);          // print head and deletes the head         System.out.println("Queue's head: " + Q.poll());          // print head and deleted the head         System.out.println("Queue's head: " + Q.poll());          // print queue         System.out.println("Queue: " + Q);          // print null as Queue is empty now         System.out.println("Queue's head: " + Q.poll());     } } 
Output:
  Queue: [423, 3432]  Queue's head: 423  Queue's head: 3432  Queue: []  Queue's head: null  
Program 3: With the help of ArrayDeque. Java
// Java Program Demonstrate poll() // 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);          // print queue         System.out.println("Queue: " + Q);          // print head and deletes the head         System.out.println("Queue's head: " + Q.poll());          // print head and deleted the head         System.out.println("Queue's head: " + Q.poll());     } } 
Output:
  Queue: [7855642, 35658786, 5278367, 74381793]  Queue's head: 7855642  Queue's head: 35658786  
Program 4: With the help of ConcurrentLinkedDeque. Java
// Java Program Demonstrate poll() // 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);          // print queue         System.out.println("Queue: " + Q);          // print head and deletes the head         System.out.println("Queue's head: " + Q.poll());          // print head and deleted the head         System.out.println("Queue's head: " + Q.poll());     } } 
Output:
  Queue: [7855642, 35658786, 5278367, 74381793]  Queue's head: 7855642  Queue's head: 35658786  
Program 5: With the help of LinkedBlockingDeque. Java
// Java Program Demonstrate poll() // 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);          // print queue         System.out.println("Queue: " + Q);          // print head and deletes the head         System.out.println("Queue's head: " + Q.poll());          // print head and deleted the head         System.out.println("Queue's head: " + Q.poll());     } } 
Output:
  Queue: [7855642, 35658786, 5278367, 74381793]  Queue's head: 7855642  Queue's head: 35658786  
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html#poll--

Next Article
Queue poll() 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

    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 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
    PriorityQueue poll() Method in Java
    The java.util.PriorityQueue.poll() method in Java is used to retrieve or fetch and remove the first element of the Queue or the element present at the head of the Queue. The peek() method only retrieved the element at the head but the poll() also removes the element along with the retrieval. It retu
    2 min read
    Queue add() method in Java
    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: boolea
    4 min read
    Queue remove() method in Java
    The remove() method of Queue Interface returns and removes the element at the front of the container. It deletes the head of the container. The method throws an NoSuchElementException when the Queue is empty. Syntax: E remove() Returns: This method returns the head of the Queue. Exception: The funct
    3 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