In the previous article, we have discussed about Java LinkedList pollLast() Method with Examples
In this article we are going to see the use of Java LinkedList pollFirst() method in Java along with suitable examples.
Java LinkedList pollFirst() Method with Examples
This java.util.LinkedList.pollFirst()
method is used retrieve and delete the element present in the First(head) position.
It returns the element, which is present in the First 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.pollFirst()
Where,
LinkedListName
refers to the name of your LinkedList.
Let’s see different examples to understand it more clearly.
Method-1: Java LinkedList pollFirst() 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 head element from LinkedList using
pollFirst( )
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 head element from LinkedList using specific position System.out.println("Element at first position is: " + l.pollFirst()); // Prints the LinkedList elements after pollFirst() method System.out.println("The elements of LinkedList are: "+l); } }
Output: The elements of LinkedList are: [windows, linux, android, ios, symbian] Element at first position is: windows The elements of LinkedList are: [linux, android, ios, symbian]
Method-2: Java LinkedList pollFirst() 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 head element from LinkedList using
pollFirst( )
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 head element from LinkedList using specific position System.out.println("Element at first position is: " + l.pollFirst()); // Prints the LinkedList elements after pollFirst() method System.out.println("The elements of LinkedList are: "+l); } }
Output: The elements of LinkedList are: [20, 521, 132, 173, 14] Element at first position is: 20 The elements of LinkedList are: [521, 132, 173, 14]
Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples.
Related Java Programs: