Java LinkedList size() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, size() returns the number of elements currently stored in the LinkedList. An empty list has size 0.

The list keeps this count in a field, so size() does not walk the chain and takes the same tiny amount of time for any list. It counts elements, so a null element is counted as well.

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

Call it with no arguments.

Java
LinkedList<String> list = new LinkedList<>();
int a = list.size();   // 0
list.add("A");
list.add("B");
int b = list.size();   // 2
CallWhat it does
list.size()The number of elements in the list.
list.isEmpty()Tells whether the size is 0.
array.lengthThe size of an array (a field, not a method).
string.length()The number of characters in a String.

Unlike an array, a list changes its size as you add and remove elements. A LinkedList has no separate "capacity".

Library declarations
Java
// java.util.LinkedList
public int size()

Parameters

ParameterDescription
Nonesize() takes no parameters.

Return Value

Return ValueDescription
intThe number of elements in the list.

Compare with Similar Methods

Ways to ask about size:

MethodHow it differs
isEmpty()A clearer way to ask whether the size is 0.
clear()Brings the size back to 0.
get(size() - 1)Uses the size to read the last element.
ArrayList.size()The same method on an array-backed list.

Java Examples

Example 1 — Watch the Size Change as You Add and Remove

Print the size of a list after each change.

Java
import java.util.LinkedList;

public class LinkedListSizeDemo {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        System.out.println("new list: " + list.size());

        list.add("a");
        list.add("b");
        list.add("c");
        System.out.println("after 3 adds: " + list.size());

        list.removeFirst();
        System.out.println("after removeFirst: " + list.size());

        list.clear();
        System.out.println("after clear: " + list.size());
    }
}

How It Works

  1. A new list is empty. Its size is 0.
  2. Each add grows it by one. After three adds the size is 3.
  3. A removal shrinks it by one. removeFirst() takes the size from 3 down to 2.
  4. clear resets it. After clear() the size is 0 again.

Example 2 — Use size to Loop and to Find the Last Index

Loop over a list using size(), and read the last element with size() - 1.

Java
import java.util.Arrays;
import java.util.LinkedList;

public class LinkedListSizeLoop {
    public static void main(String[] args) {
        LinkedList<String> days = new LinkedList<>(
            Arrays.asList("Mon", "Tue", "Wed", "Thu"));

        System.out.println("days: " + days.size());
        System.out.println("last index: " + (days.size() - 1));
        System.out.println("last day: " + days.get(days.size() - 1));

        for (int i = 0; i < days.size(); i++) {
            System.out.println(i + " -> " + days.get(i));
        }
    }
}

How It Works

  1. Four days. The size is 4.
  2. The last index is one less. Indexes run from 0 to 3, so size() - 1 is 3.
  3. Reading the last day. get(3) returns "Thu".
  4. The loop runs while i is below size(). It prints the indexes 0, 1, 2 and 3, and stops before 4. For a very long LinkedList, prefer a for-each loop.

Example 3 — See That null Elements Are Counted

Add nulls and duplicates and notice that size() counts every element.

Java
import java.util.LinkedList;

public class LinkedListSizeNulls {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("x");
        list.add("x");
        list.add(null);
        list.add(null);

        System.out.println("list: " + list);
        System.out.println("size: " + list.size());

        list.remove(null);
        System.out.println("after removing one null: " + list.size());
    }
}

How It Works

  1. Four elements. Two are "x" and two are null.
  2. Duplicates count separately. A LinkedList does not merge equal values, so both "x" entries count.
  3. Nulls count too. size() reports 4, because every stored element is counted, even a null.
  4. One null removed. remove(null) deletes the first null only, so the size becomes 3.

Frequently Asked Questions

Call list.size(). It returns the count as an int.
No. The list stores its size in a field, so size() takes constant time however many elements there are.
size() is a method on collections such as LinkedList. length is a field of arrays, and length() is a method of String.
size() - 1. An index equal to size() is out of range for get and set.
Yes. Every stored element counts, including null and duplicate values.

Next: sort()

Sort the list in place, using natural order or a Comparator.

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

10 people found this page helpful