Java LinkedList peekLast() Method with Examples

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

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

Java LinkedList peekLast() Method with Examples

This java.util.LinkedList.peekLast() method is used retrieve the element present in the Last(tail) position of the LinkedList.

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

Syntax:

LinkedListName.peekLast()

Where,

  • LinkedListName refers to the name of your LinkedList.

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

Method-1: Java LinkedList peekLast() 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 tail element from the LinkedList using peekLast( ) 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 last element from LinkedList 
        System.out.println("Element at Last position is: " + l.peekLast());
    }
}
Output:

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

Method-2: Java LinkedList peekLast() 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 tail element from the LinkedList using peekLast( ) 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 last element from LinkedList
        System.out.println("Element at Last position is: " + l.peekLast());
    }
}
Output:

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

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Related Java Programs: