Linkedlist poll java – Java LinkedList poll() Method with Examples

Linkedlist poll java: In the previous article, we have discussed about Java LinkedList pollFirst() Method with Examples

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

Java LinkedList poll() Method with Examples

Java linkedlist poll: This java.util.LinkedList.poll() method is used retrieve and delete the element present in the first position of LinkedList.

It returns the element, which is present in the first position of the LinkedList else returns null if there is no element in the LinkedList.

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

Syntax:

LinkedListName.poll()

Where,

  • LinkedListName refers to the name of your LinkedList.

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

Method-1: Java LinkedList poll() 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 remove first element from the LinkedList using poll( ) 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("Hello");
        l.add("this");
        l.add("is");
        l.add("an");
        l.add("example of poll() method");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // retrieve and remove first element from LinkedList 
        System.out.println("Element at first position is: " + l.poll());
        // Prints the LinkedList elements after poll() method
        System.out.println("The elements of LinkedList are: "+l);
    }
}

Output:

The elements of LinkedList are: [Hello, this, is, an, example of poll() method]
Element at first position is: Hello
The elements of LinkedList are: [this, is, an, example of poll() method]

Method-2: Java LinkedList poll() 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 remove first element from the LinkedList using poll( ) 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(2);
        l.add(52);
        l.add(13);
        l.add(17);
        l.add(1);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        //  retrieve and remove first element from LinkedList 
        System.out.println("Element at first position is: " + l.poll());
        // Prints the LinkedList elements after poll() method
        System.out.println("The elements of LinkedList are: "+l);
    }
}
Output:

The elements of LinkedList are: [2, 52, 13, 17, 1]
Element at first position is: 2
The elements of LinkedList are: [52, 13, 17, 1]

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Related Java Programs: