Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
Last Updated :
10 Dec, 2018
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.
import java.util.*;
public class LinkedListPollLast {
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.add( "Geeks" );
list.add( 4 );
list.add( "Geeks" );
list.add( 8 );
System.out.println( "The initial Linked List is : " + list);
System.out.println( "Tail element of the list is : " + list.pollLast());
System.out.println( "Linked List after removal using pollLast() : " + list);
}
}
|
Output :
The initial Linked List is : [Geeks, 4, Geeks, 8]
Head element of the list is : Geeks
Linked List after removal using poll() : [4, Geeks, 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
import java.util.*;
public class LinkedListPollApp {
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.add( "Astha" );
list.add( "Shambhavi" );
list.add( "Nikhil" );
list.add( "Manjeet" );
System.out.println( "The initial queue is : " + list);
System.out.print( "The order of exit is : " );
while (!list.isEmpty()) {
System.out.print(list.poll() + " <-- " );
}
}
}
|
Output :
The initial Linked List is : [Geeks, 4, Geeks, 8]
Head element of the list is : Geeks
Linked List after removal using pollFirst() : [4, Geeks, 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.
Output :
The initial Linked List is : [Geeks, 4, Geeks, 8]
Tail element of the list is : 8
Linked List after removal using pollLast() : [Geeks, 4, Geeks]
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.
import java.util.*;
public class LinkedListPollApp {
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.add( "Astha" );
list.add( "Shambhavi" );
list.add( "Nikhil" );
list.add( "Manjeet" );
System.out.println( "The initial queue is : " + list);
System.out.print( "The order of exit is : " );
while (!list.isEmpty()) {
System.out.print(list.poll() + " <-- " );
}
}
}
|
Output :
The initial queue is : [Astha, Shambhavi, Nikhil, Manjeet]
The order of exit is : Astha <-- Shambhavi <-- Nikhil <-- Manjeet <--
Similar Reads
LinkedList addLast() Method in Java
In Java, the addLast() method of the LinkedList class is used to add an element at the end of the list. Syntax of LinkedList addLast() Method void addLast( E e) Parameter: e is the element you want to add at the end of the list.Return type: This method does not return any value.Example: Here, we use
1 min read
LinkedList clear() Method in Java
In Java, the clear() is used to remove all the elements from a LinkedList. This method only clears all the element from the list and not deletes the list. After calling this method, the list will be empty. Syntax of LinkedList clear() Methodvoid clear() Parameters: This method does not accept any pa
1 min read
LinkedList clone() Method in Java
In Java, the clone() method of LinkedList, creates a shallow copy which means the structure is duplicated but the objects inside the list are shared between the original and the copied list. So, the cloned list will have the same elements as the original list. Syntax of Java LinkedList clone() Metho
1 min read
LinkedList contains() Method in Java
In Java, the contains() method of LinkedList is used to check whether an element is present in a LinkedList or not. It takes the element as a parameter and returns True if the element is present in the list. Syntax of Java LinkedList contains() Method boolean contains(Object element);Parameter: The
2 min read
LinkedList descendingIterator() Method in Java
In Java, the descendingIterator() method of LinkedList is used to return an iterator that allows to traverse the list in reverse order. Example 1: This example demonstrates how to use descendingIterator() method with Strings in a LinkedList. [GFGTABS] Java // Java program to use // descendingIterato
3 min read
LinkedList element() Method in Java
In Java, the element() method of the LinkedList class is used to retrieve the first element in the list without removing it. The first element of the LinkedList is known as the head. Example 1: Here, we use the element() method to retrieve the first element of the LinkedList of Integers, without rem
2 min read
Java.util.LinkedList.get(), getFirst(), getLast() in Java
In Java, the LinkedList class provides several methods for accessing the elements in the list. Here are the methods for getting the elements of a LinkedList: get(int index): This method returns the element at the specified position in the LinkedList. The index parameter is zero-based, so the first e
5 min read
LinkedList getLast() Method in Java
In Java, the getLast() method in the LinkedList class is used to retrieve the last element of the list. Example 1: Here, we use the getLast() method to retrieve the last element of the list. [GFGTABS] Java // Java Program to Demonstrate the // use of getLast() in LinkedList import java.util.LinkedLi
2 min read
LinkedList indexOf() Method in Java
In Java, the indexOf() method of the LinkedList class is used to find the index of the first occurrence of the specified element in the list. Syntax of LinkedList indexOf() Method public int indexOf(Object o); Parameter: This method takes an object "o" as an argument. Return type: It returns the ind
2 min read
LinkedList lastIndexOf() Method in Java
In Java, the lastIndexOf() method of LinkedList is used to find the last occurrence of the specified element in the list. If the element is present it will return the last occurrence of the element, otherwise, it will return -1. Syntax of LinkedList lastIndexOf() Method public int lastIndexOf(Object
2 min read