Linkedlist peek – Java LinkedList peek() Method with Examples

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

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

Java LinkedList peek() Method with Examples

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

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.peek()

Where,

  • LinkedListName refers to the name of your LinkedList.

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

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

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

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

Output:

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

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: