Java LinkedList peekFirst() Method with Examples

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

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

Java LinkedList peekFirst() Method with Examples

This java.util.LinkedList.peekFirst() method is used retrieve the element present in the First(head) position of the LinkedList. It works similar to the peek() method.

It returns the element, which is present in the First position of the LinkedList and if the list has no element then it returns null.

Syntax:

LinkedListName.peekFirst()

Where,

  • LinkedListName refers to the name of your LinkedList.

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

Method-1: Java LinkedList peekFirst() 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 peek the head element from the LinkedList using peekFirst( ) 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);
        // peek first element from LinkedList 
       System.out.println("Element at first position is: " + l.peekFirst());
    }
}
Output:

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

Method-2: Java LinkedList peekFirst() 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 peek the head element from the LinkedList using peekFirst( )┬á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);
        // peek first element from LinkedList
       System.out.println("Element at first position is: " + l.peekFirst());
    }
}

Output:

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

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Related Java Programs: