Java LinkedList poll() pollFirst() pollLast() Methods Tutorial

In this section we will learn what the LinkedList poll(), pollFirst(), and pollLast() methods are and how to use them in Java.

What is Java LinkedList poll() Method?

The Java LinkedList poll() method is used to get the first element of a LinkedList object and returns that as a result.

Note: this method removes the first element as well.

Java poll() Method Syntax:

public E poll()

poll() Method Parameters:

The method does not take an argument.

poll() Method Return Value:

The return value of this method is the first element (head) of the target LinkedList object.

Note: the value null returns if the list is empty.

poll() Method Exceptions:

The method does not throw an exception.

Example: using LinkedList poll() method

import java.util.LinkedList; 
class Main{
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.offer("John");
        list.offer("Omid");
        list.offer("Jack");
        list.offer("Ellen");
        list.offer("Elon");

        System.out.println("The size of the list before calling the method: "+list.size());
        System.out.println(list.poll());
        System.out.println("The size of the list after calling the method: "+list.size());
    }
}

Output:

The size of the list before calling the method: 5

John

The size of the list after calling the method: 4

What is Java LinkedList pollFirst() Method?

The Java LinkedList pollFirst() method is used to take the first (head) element of a LinkedList object.

Note: the method removes that first element from the list as well.

Java pollFirst() Method Syntax:

public E pollFirst()

pollFirst() Method Parameters:

The method does not take an argument.

pollFirst() Method Return Value:

The method returns the first (head) element of the target LinkedList object.

Note: the value null returns if the list is empty.

pollFirst() Method Exceptions:

The method does not throw an exception.

Example: using LinkedList pollFirst() method

import java.util.LinkedList; 
class Main{
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.offer("John");
        list.offer("Omid");
        list.offer("Jack");
        list.offer("Ellen");
        list.offer("Elon");

        System.out.println("The size of the list before calling the method: "+list.size());
        System.out.println(list.pollFirst());
        System.out.println("The size of the list after calling the method: "+list.size());
    }
}

Output:

The size of the list before calling the method: 5

John

The size of the list after calling the method: 4

What is Java LinkedList pollLast() Method?

The Java LinkedList pollLast() method is used to get the last element of a LinkedList object and returns that as a result.

Note: the method removes the last element as well.

Java pollLast() Method Syntax:

public E pollLast()

pollLast() Method Parameters:

The method takes no argument.

pollLast() Method Return Value:

The return value of this method is the last element of the target LinkedList object.

Note: the value null returns if the list is empty.

pollLast() Method Exceptions:

The method does not throw an exception.

Example: using LinkedList pollLast() method

import java.util.LinkedList; 
class Main{
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.offer("John");
        list.offer("Omid");
        list.offer("Jack");
        list.offer("Ellen");
        list.offer("Elon");

        System.out.println("The size of the list before calling the method: "+list.size());
        System.out.println(list.pollLast());
        System.out.println("The size of the list after calling the method: "+list.size());
    }
}

Output:

The size of the list before calling the method: 5

Elon

The size of the list after calling the method: 4
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies