Java LinkedList get() getFirst() getLast() Methods Tutorial

In this section we will learn what the LinkedList get(), getFirst(), and getLast() methods are and how to use them in Java.

What is Java LinkedList get() Method?

The Java LinkedList get() method is used to get an element from a LinkedList object at a specific index.

Basically, this method takes an index number and returns the current element there as a result.

Java get() Method Syntax:

public E get(int index)

get() Method Parameters:

The method takes one argument and that is an index number that we want to get the current element there.

get() Method Return Value:

The return value of this method is the element at the specified index.

get() Method Exceptions:

The method might throw one exception and that is:

IndexOutOfBoundsException: we get this exception if the argument of the method is out of bound in the target LinkedList object.

Example: using LinkedList get() method

import java.util.LinkedList; 
class Main{
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("John");
        list.add("Omid");
        list.add("Jack");
        list.add("Ellen");
        list.add("Elon");

        System.out.println(list.get(1));
        System.out.println(list.get(2));
    }
}

Output:

Omid

Jack

What is Java LinkedList getFirst() Method?

The Java LinkedList getFirst() method is used to get the first (head) element of a LinkedList object and return that as a result.

Note: this method does not remove the head element.

Java getFirst() Method Syntax:

public E getFirst()

getFirst() Method Parameters:

The method does not take an argument.

getFirst() Method Return Value:

The return value of this method is the first (head) element of the target LinkedList object.

getFirst() Method Exceptions:

The method might throw one exception and that is:

NoSuchElementException: We get this exception if the target LinkedList object is empty and we’ve called this method, anyway.

Example: using LinkedList getFirst() method

import java.util.LinkedList; 
class Main{
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("John");
        list.add("Omid");
        list.add("Jack");
        list.add("Ellen");
        list.add("Elon");

        System.out.println(list.getFirst());
    }
}

Output:

John

What is Java LinkedList getLast() Method?

The Java LinkedList getLast() method is used to get the last (tail) element of a LinkedList object and return that as a result.

Note: this method does not remove the last element.

Java getLast() Method Syntax:

public E getLast()

getLast() Method Parameters:

The method does not take an argument.

getLast() Method Return Value:

The return value of this method is the last element of the target LinkedList object.

getLast() Method Exceptions:

The method might throw one exception and that is:

NoSuchElementException: We get this exception if the target LinkedList object is empty and we’ve called this method, anyway.

Example: using LinkedList getLast() method

import java.util.LinkedList; 
class Main{
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("John");
        list.add("Omid");
        list.add("Jack");
        list.add("Ellen");
        list.add("Elon");

        System.out.println(list.getLast());
    }
}

Output:

Elon

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies