Getlast – Java LinkedList getLast() Method with Examples

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

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

Java LinkedList getLast() Method with Examples

This java.util.LinkedList.getLast() method is used retrieve the element present in the last/tail position of LinkedList.

It returns the element, which is present in the tail of the LinkedList.

Syntax:

LinkedListName.getLast()

Where,

  • LinkedListName refers to the name of your LinkedList.

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

Example-1: Java LinkedList getLast() 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 get last element from the LinkedList using getLast( ) 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 getLast() method");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // get last element from the LinkedList 
       System.out.println("Element at Last position is: " + l.getLast());
    }
}

Output:

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

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

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

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Related Java Programs: