What is a Queue?
Just like the name implies, Queue is a common data structure in Java following the First In First Out (FIFO) insertion order. You can easily imagine it as a queue at a grocery store. The sooner you enter, the sooner you leave. It means the element added to the queue earlier will leave earlier as well. The first element (front) of the queue is also called head.
What is the poll() method of Queue?
The poll() method allows you to retrieve and remove the top most element (head) of a Queue. In our example, when you call poll() at the above queue, you’ll receive ‘1’ as a result. And there will be only 4 elements remaining in the queue.
poll() method in Java
Example 1
Have a look at a simple example of calling poll() function on the queue shown in the figure 1.0.
import java.util.LinkedList;
import java.util.Queue;
public class QueuePollMethod {
public static void main(String[] args) {
// create a queue of die rolls
Queue dieRoll = new LinkedList();
// Add 6 integers one by one
dieRoll.add(1);
dieRoll.add(2);
dieRoll.add(3);
dieRoll.add(4);
dieRoll.add(5);
dieRoll.add(6);
// print the original queue
System.out.println("Queue:\t" + dieRoll + "\n");
// after calling poll()
System.out.println("poll() returned : " + dieRoll.poll());
System.out.println("Queue Updated!\t" + dieRoll + "\n");
// after calling poll()
System.out.println("poll() returned : " + dieRoll.poll());
System.out.println("Queue Updated!\t" + dieRoll + "\n");
// after calling poll()
System.out.println("poll() returned : " + dieRoll.poll());
System.out.println("Queue Updated!\t" + dieRoll + "\n");
}
}
Output
Queue: [1, 2, 3, 4, 5, 6]
poll() returned : 1
Queue Updated! [2, 3, 4, 5, 6]
poll() returned : 2
Queue Updated! [3, 4, 5, 6]
poll() returned : 3
Queue Updated! [4, 5, 6]
Please note, how after each poll() call size of the queue is reduced by 1 and head is returned.Example 2
Let’s look at another example of days in a week. We’ll call poll() one by one on weekdays and see what it looks like.
import java.util.LinkedList;
import java.util.Queue;
public class QueuePollMethod {
public static void main(String[] args) {
Queue days = new LinkedList();
days.add("Sunday");
days.add("Monday");
days.add("Tuesday");
days.add("Wednesday");
days.add("Thursday");
days.add("Friday");
days.add("Saturday");
// print all the days in the week
System.out.println("Week Days: \t" + days + "\n");
// after calling poll()
System.out.println("poll() returned: " + days.poll());
System.out.println("Days Updated!\t" + days + "\n");
// after calling poll()
System.out.println("poll() returned: " + days.poll());
System.out.println("Days Updated!\t" + days + "\n");
// after calling poll()
System.out.println("poll() returned: " + days.poll());
System.out.println("Days Updated!\t" + days + "\n");
}
}
Output
Week Days: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
poll() returned: Sunday
Days Updated! [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
poll() returned: Monday
Days Updated! [Tuesday, Wednesday, Thursday, Friday, Saturday]
poll() returned: Tuesday
Days Updated! [Wednesday, Thursday, Friday, Saturday]
How poll() is different from peek() and remove()?
It is very easy for beginners to mingle up the three different concepts since they look quite similar. Let’s “q” be a queue, then here’s how they are different:- q.poll(): removes and retrieves the head of the queue
- q.peek(): does not remove but only retrieves/returns the head of the queue
- q.remove(): removes and retrieves the head of the queue
Example 3
Now let’s have a quick look on a few examples of the above three functions.
import java.util.LinkedList;
import java.util.Queue;
public class QueuePollMethod {
public static void main(String[] args) {
Queue days = new LinkedList();
days.add("Sunday");
days.add("Monday");
days.add("Tuesday");
days.add("Wednesday");
days.add("Thursday");
days.add("Friday");
days.add("Saturday");
// print all the days in the week
System.out.println("Week Days: \t" + days + "\n");
// after calling peek()
System.out.println("peek() returned: " + days.peek());
System.out.println("Week Days: \t" + days + "\n");
System.out.println("peek() returned: " + days.peek());
System.out.println("Week Days: \t" + days + "\n");
// after calling remove()
System.out.println("remove() returned: " + days.remove());
System.out.println("Days Updated!\t" + days + "\n");
System.out.println("remove() returned: " + days.remove());
System.out.println("Days Updated!\t" + days + "\n");
System.out.println("remove() returned: " + days.remove());
System.out.println("remove() returned: " + days.remove());
System.out.println("remove() returned: " + days.remove());
System.out.println("remove() returned: " + days.remove());
System.out.println("Days Updated!\t" + days + "\n");
// after calling poll()
System.out.println("poll() returned: " + days.poll());
System.out.println("Days Updated!\t" + days + "\n");
System.out.println("poll() returned: " + days.poll());
System.out.println("remove() returned: " + days.remove());
}
}
Output
Week Days: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
peek() returned: Sunday
Week Days: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
peek() returned: Sunday
Week Days: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
remove() returned: Sunday
Days Updated! [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
remove() returned: Monday
Days Updated! [Tuesday, Wednesday, Thursday, Friday, Saturday]
remove() returned: Tuesday
remove() returned: Wednesday
remove() returned: Thursday
remove() returned: Friday
Days Updated! [Saturday]
poll() returned: Saturday
Days Updated! []
poll() returned: null
Exception in thread "main" java.util.NoSuchElementException
at java.util.LinkedList.removeFirst(LinkedList.java:270)
at java.util.LinkedList.remove(LinkedList.java:685)
at QueuePollMethod.main(QueuePollMethod.java:48)
As you can see after calling peek() weekdays remain the same. And as we use remove() or poll() the size of the queue is reduced by 1 alongwith returning the head. Moreover, java.util.NoSuchElementException is thrown when we call remove() at an empty queue, while no such exception is thrown for using poll().
GO TO FULL VERSION