Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
Java’s Linked list class offers a function that allows a “Queue Based” working called poll(). This function not only returns deletes the first element, but also displays them while being deleted and hence can have a lot of usage in daily life problems and competitive programming as well. There are 3 variants of poll(), all three are discussed in this article.
1. poll() : This method retrieves and removes the head (first element) of this list.
Declaration : public E poll() Return Value : This method returns the first element of this list, or null if this list is empty.
// Java code to demonstrate the working // of poll() in linked list import java.util.*; public class LinkedListPoll { public static void main(String[] args) { // Declaring a LinkedList LinkedList list = new LinkedList(); // adding elements list.add( "Beginner" ); list.add( 4 ); list.add( "Beginner" ); list.add( 8 ); // printing the list System.out.println( "The initial Linked List is : " + list); // using poll() to retrieve and remove the head // removes and displays "Beginner" System.out.println( "Head element of the list is : " + list.poll()); // printing the resultant list System.out.println( "Linked List after removal using poll() : " + list); } } |
Output :
The initial Linked List is : [Beginner, 4, Beginner, 8] Head element of the list is : Beginner Linked List after removal using poll() : [4, Beginner, 8]
2. pollFirst() : This method retrieves and removes the first element of this list, or returns null if this list is empty.
Declaration : public E pollFirst() Return Value : This method returns the first element of this list, or null if this list is empty
// Java code to demonstrate the working // of pollFirst() in linked list import java.util.*; public class LinkedListPollFirst { public static void main(String[] args) { // Declaring a LinkedList LinkedList list = new LinkedList(); // adding elements list.add( "Beginner" ); list.add( 4 ); list.add( "Beginner" ); list.add( 8 ); // printing the list System.out.println( "The initial Linked List is : " + list); // using pollFirst() to retrieve and remove the head // removes and displays "Beginner" System.out.println( "Head element of the list is : " + list.pollFirst()); // printing the resultant list System.out.println( "Linked List after removal using pollFirst() : " + list); } } |
Output :
The initial Linked List is : [Beginner, 4, Beginner, 8] Head element of the list is : Beginner Linked List after removal using pollFirst() : [4, Beginner, 8]
3. pollLast() : This method retrieves and removes the last element of this list, or returns null if this list is empty.
Declaration : public E pollLast() Return Value : This method returns the last element of this list, or null if this list is empty.
// Java code to demonstrate the working // of pollLast() in linked list import java.util.*; public class LinkedListPollLast { public static void main(String[] args) { // Declaring a LinkedList LinkedList list = new LinkedList(); // adding elements list.add( "Beginner" ); list.add( 4 ); list.add( "Beginner" ); list.add( 8 ); // printing the list System.out.println( "The initial Linked List is : " + list); // using pollLast() to retrieve and remove the tail // removes and displays 8 System.out.println( "Tail element of the list is : " + list.pollLast()); // printing the resultant list System.out.println( "Linked List after removal using pollLast() : " + list); } } |
Output :
The initial Linked List is : [Beginner, 4, Beginner, 8] Tail element of the list is : 8 Linked List after removal using pollLast() : [Beginner, 4, Beginner]
Practical Application : This function has potential usage in the “queue management” systems and also in “1st elimination” games that can be thought of. The former example is discussed below.
// Java code to demonstrate the practical // application of poll() in linked list import java.util.*; public class LinkedListPollApp { public static void main(String[] args) { // Declaring a LinkedList LinkedList list = new LinkedList(); // adding queue entry of people // in order list.add( "Astha" ); list.add( "Shambhavi" ); list.add( "Nikhil" ); list.add( "Manjeet" ); // printing the list System.out.println( "The initial queue is : " + list); System.out.print( "The order of exit is : " ); while (!list.isEmpty()) { // using poll() to display the order of exit from queue System.out.print(list.poll() + " <-- " ); } } } |
Output :
The initial queue is : [Astha, Shambhavi, Nikhil, Manjeet] The order of exit is : Astha <-- Shambhavi <-- Nikhil <-- Manjeet <--
Contact Us