Java LinkedList poll, pollFirst and pollLast example

Introduction :

Java provides a utility class called LinkedList to create linked lists. Unlike any other programming languages like C, we can create one Linked list easily using this class.

This class also provides a lot of public methods to operate on the linked list elements. poll(), pollFirst() and pollLast() are such methods to remove elements from a linked list. In this tutorial, we will check how these methods actually work.

poll() :

This method is used to remove the head or first element of a linked list. It returns the head of the list or if the list is empty, it returns null. For example :

import java.util.LinkedList;
public class Main {
    public static void main(String[] args){
        //1
        LinkedList<string> firstList = new LinkedList<>();
        LinkedList<string> secondList = new LinkedList<>();
        //2
        firstList.add("first");
        firstList.add("second");
        firstList.add("third");
        firstList.add("fourth");
        firstList.add("fifth");
        //3
        System.out.println("Before : "+firstList);
        //4
        System.out.println("Polling from the first list : "+firstList.poll());
        System.out.println("Polling from the second list : "+secondList.poll());
        //5
        System.out.println("After : "+firstList);
    }
}

Explanation :

The commented numbers in the above program denote the step numbers below :

  1. We have created two LinkedList variables at the start of the program: firstList and secondList.

  2. Add five different string elements to the first linked list firstList. We are not adding anything to the other list secondList.

  3. Print the values of firstList to the user.

  4. Use poll on both of these lists. Note that secondList is null here.

  5. Print out the firstList to the user.

Output :

Before : [first, second, third, fourth, fifth]
Polling from the first list : first
Polling from the second list : null
After : [second, third, fourth, fifth]

As you can see here that the item is removed from the list and it was returned by the poll() method. null is returned for the empty list.

pollFirst() :

This method retrieves and removes the first element of the list and returns null for an empty list.

For example :

import java.util.LinkedList;
public class Main {
    public static void main(String[] args){
        LinkedList<string> firstList = new LinkedList<>();
        LinkedList<string> secondList = new LinkedList<>();
        firstList.add("first");
        firstList.add("second");
        firstList.add("third");
        firstList.add("fourth");
        firstList.add("fifth");
        System.out.println("Before : "+firstList);
        System.out.println("Polling from the first list : "+firstList.pollFirst());
        System.out.println("Polling from the second list : "+secondList.pollFirst());
        System.out.println("After : "+firstList);
    }
}

Output :

Before : [first, second, third, fourth, fifth]
Polling from the first list : first
Polling from the second list : null
After : [second, third, fourth, fifth]

As you can see here, the first element is removed from the linked list.

pollLast() :

pollLast() is used for removing the last element from a linked list :

import java.util.LinkedList;
public class Main {
    public static void main(String[] args){
        LinkedList<string> firstList = new LinkedList<>();
        LinkedList<string> secondList = new LinkedList<>();
        firstList.add("first");
        firstList.add("second");
        firstList.add("third");
        firstList.add("fourth");
        firstList.add("fifth");
        System.out.println("Before : "+firstList);
        System.out.println("Polling from the first list : "+firstList.pollLast());
        System.out.println("Polling from the second list : "+secondList.pollLast());
        System.out.println("After : "+firstList);
    }
}

Output :

Before : [first, second, third, fourth, fifth]
Polling from the first list : fifth
Polling from the second list : null
After : [first, second, third, fourth]

Similar tutorials :