Java LinkedList pollLast() Method with Examples

In the previous article, we have discussed about Java LinkedList retainAll() Method with Examples

In this article we are going to see the use of Java LinkedList pollLast() method in Java along with suitable examples.

Java LinkedList pollLast() Method with Examples

This java.util.LinkedList.pollLast() method is used to retrieve and delete the element present in the last(tail) position.

It returns the element, which is present in the last position of the LinkedList or returns null if the list is empty.

If the index is out of the size/range of the LinkedList then it shows IndexOutOfBoundException.

Syntax:

LinkedListName.pollLast()

Where,

  • LinkedListName refers to the name of your LinkedList.

Let’s see different examples to understand it more clearly.

Method-1: Java LinkedList pollLast() Method – Example with String Type LinkedList

Approach:

  • Create a new LinkedList of type String.
  • Add string elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Now retrieve and delete the tail element from LinkedList using pollLast( ) method.
  • Print the element.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of string datatype
        LinkedList<String> l = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l.add("windows");
        l.add("linux");
        l.add("android");
        l.add("ios");
        l.add("symbian");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // remove tail element from LinkedList using specific position
        System.out.println("Element at Last position is: " + l.pollLast());
        // Prints the LinkedList elements after pollLast() method
        System.out.println("The elements of LinkedList are: "+l);
    }
}

Output:

The elements of LinkedList are: [windows, linux, android, ios, symbian]
Element at Last position is: symbian
The elements of LinkedList are: [windows, linux, android, ios]

Method-2: Java LinkedList pollLast() Method – Example with Integer Type LinkedList

Approach:

  • Create a new LinkedList of type Integer.
  • Add integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Now retrieve and delete the tail element from LinkedList using pollLast( ) method.
  • Print the element.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of Integer datatype
        LinkedList<Integer> l = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l.add(20);
        l.add(521);
        l.add(132);
        l.add(173);
        l.add(14);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // remove tail element from LinkedList using specific position
        System.out.println("Element at Last position is: " + l.pollLast());
        // Prints the LinkedList elements after pollLast() method
        System.out.println("The elements of LinkedList are: "+l);
    }
}

Output:

The elements of LinkedList are: [20, 521, 132, 173, 14]
Element at Last position is: 14
The elements of LinkedList are: [20, 521, 132, 173]

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Related Java Programs: