LinkedList poll() method with example in java

Published by user on

The poll() method of LinkedList retrieves and removes the first element of this linked list.

Syntax

Object poll()

Parameters

It does not take any parameter

Return Value

It returns the removed element from this linked-list

Program

import java.util.LinkedList;

public class LinkedListPollExample {

        public static void main(String[] args) {

                LinkedList oddNumbers = new LinkedList();
                oddNumbers.add(5);
                oddNumbers.add(7);
                oddNumbers.add(9);
                oddNumbers.add(11);

                System.out.println("Linked List elements: " + oddNumbers);
                System.out.println("first element removed: " + oddNumbers.poll());
                System.out.println("Linked List  elements after removing first element: " + oddNumbers);
        }
}

Output

Linked List elements: [5, 7, 9, 11]
first element removed: 5
Linked List  elements after removing first element: [7, 9, 11]
Categories: Java

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *