Java LinkedList get() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, get(index) returns the element stored at a position in the LinkedList. Positions start at 0, so the first element is at index 0 and the last one is at size() - 1.

The element stays in the list. Unlike an ArrayList, a LinkedList has no array to jump into: Java must walk the chain of nodes to reach the position, starting from whichever end is closer.

An instance method of java.util.LinkedList (since Java 1.2): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by List, and LinkedList implements it.

Syntax

Pass a valid index.

Java
LinkedList<String> list = new LinkedList<>();
list.add("A");
list.add("B");
list.add("C");
String first = list.get(0);               // "A"
String last = list.get(list.size() - 1);  // "C"
CallWhat it does
list.get(i)Returns the element at index i. The list is not changed.
list.getFirst()Returns the first element, with no index.
list.getLast()Returns the last element, with no index.
list.set(i, e)Replaces the element at i instead of reading it.

The cost of get(i) grows with the distance from the nearest end. Reaching the middle of a long list is the slowest case.

Library declarations
Java
// java.util.LinkedList
public E get(int index)

Parameters

ParameterDescription
index (int)The position to read, from 0 to size() - 1.

Return Value

Return ValueDescription
EThe element at that index. It can be null if a null was stored there.

Compare with Similar Methods

Ways to read an element:

MethodHow it differs
getFirst()Reads the head without an index. Throws if the list is empty.
peekFirst()Reads the head, but returns null on an empty list.
indexOf(o)Goes the other way: finds the index of a value.
ArrayList.get()The same call on an array-backed list, which reaches any index instantly.

Java Examples

Example 1 — Read Elements by Index

Create a list of planets and read the first, a middle, and the last one.

Java
import java.util.LinkedList;

public class LinkedListGetDemo {
    public static void main(String[] args) {
        LinkedList<String> planets = new LinkedList<>();
        planets.add("Mercury");
        planets.add("Venus");
        planets.add("Earth");
        planets.add("Mars");

        System.out.println("index 0: " + planets.get(0));
        System.out.println("index 2: " + planets.get(2));
        System.out.println("last:    " + planets.get(planets.size() - 1));
        System.out.println("list:    " + planets);
    }
}

How It Works

  1. Zero-based positions. Mercury is at 0, Venus at 1, Earth at 2, and Mars at 3.
  2. Reading index 2. The third element, Earth, is returned.
  3. The last index is size minus one. The list has four elements, so size() - 1 is 3 and get(3) is Mars.
  4. Nothing is removed. The final line shows all four planets still in the list.

Example 2 — Compare an Index Loop with a For-Each Loop

Add up the same numbers with get(i) and with for-each, and see that both give one answer.

Java
import java.util.LinkedList;

public class LinkedListGetLoop {
    public static void main(String[] args) {
        LinkedList<Integer> scores = new LinkedList<>();
        for (int i = 1; i <= 5; i++) {
            scores.add(i * 12);
        }

        int byIndex = 0;
        for (int i = 0; i < scores.size(); i++) {
            byIndex += scores.get(i);
        }

        int byEach = 0;
        for (int s : scores) {
            byEach += s;
        }

        System.out.println("scores: " + scores);
        System.out.println("sum with get(i): " + byIndex);
        System.out.println("sum with for-each: " + byEach);
    }
}

How It Works

  1. Five scores. The loop adds 12, 24, 36, 48 and 60.
  2. Index loop. get(i) works, but each call must walk the chain from an end to position i again.
  3. For-each loop. It uses an iterator that moves from one node to the next, visiting each node once.
  4. Same total, different cost. Both sums are 180. On a list of a million items the for-each version is far faster.

Example 3 — Handle an Index That Is Too Large

Try the last valid index, then an index equal to the size, and a negative index.

Java
import java.util.LinkedList;

public class LinkedListGetBounds {
    public static void main(String[] args) {
        LinkedList<String> seats = new LinkedList<>();
        seats.add("A1");
        seats.add("A2");
        seats.add("A3");

        System.out.println("last valid: " + seats.get(2));

        try {
            seats.get(3);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("get(3) -> IndexOutOfBoundsException");
        }

        try {
            seats.get(-1);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("get(-1) -> IndexOutOfBoundsException");
        }
    }
}

How It Works

  1. Three seats. The valid indexes are 0, 1 and 2.
  2. Index 2 works. It is the last position, and returns A3.
  3. Index 3 is one too many. An index equal to size() points past the end, so Java throws IndexOutOfBoundsException.
  4. Negative indexes fail too. There is no counting from the end in Java, so get(-1) throws the same exception.

Frequently Asked Questions

Call list.get(index). The first element is at index 0 and the last is at size() - 1.
It can be. A LinkedList has to walk its chain of nodes to reach the index, so the time grows with the position. An ArrayList reaches any index instantly.
get throws an IndexOutOfBoundsException when the index is negative or equal to or larger than size().
No. It only reads the element. Use remove(index) to read and delete it in one step.
Use a for-each loop or an iterator. They visit each node once, while get(i) in a loop repeats the walk for every index.

Next: getFirst()

Read the first element, or throw if the list is empty.

getFirst() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful