Home Java LinkedList Methods size() Java LinkedList size() Method 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.
LinkedList<String> list = new LinkedList<>();
int a = list.size(); // 0
list.add("A");
list.add("B");
int b = list.size(); // 2 Call What 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.util.LinkedList
public int size() Parameters Parameter Description None size() takes no parameters.
Return Value Return Value Description intThe number of elements in the list.
Important
The last valid index is size() - 1, not size(). The size can change while you loop, so if you remove elements inside a loop, recompute or use an iterator. A LinkedList never needs resizing, so there is no ensureCapacity or trimToSize like on ArrayList.
Compare with Similar Methods Ways to ask about size:
Java Examples 1. Count the elements 2. Loop and last index 3. Counts nulls too Example 1 — Watch the Size Change as You Add and Remove Print the size of a list after each change.
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());
}
} new list: 0
after 3 adds: 3
after removeFirst: 2
after clear: 0 How It Works A new list is empty. Its size is 0.Each add grows it by one. After three adds the size is 3.A removal shrinks it by one. removeFirst() takes the size from 3 down to 2.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.
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));
}
}
} days: 4
last index: 3
last day: Thu
0 -> Mon
1 -> Tue
2 -> Wed
3 -> Thu How It Works Four days. The size is 4.The last index is one less. Indexes run from 0 to 3, so size() - 1 is 3.Reading the last day. get(3) returns "Thu".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.
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());
}
} list: [x, x, null, null]
size: 4
after removing one null: 3 How It Works Four elements. Two are "x" and two are null.Duplicates count separately. A LinkedList does not merge equal values, so both "x" entries count.Nulls count too. size() reports 4, because every stored element is counted, even a null.One null removed. remove(null) deletes the first null only, so the size becomes 3.Frequently Asked Questions How do I get the number of elements in a LinkedList? Call list.size(). It returns the count as an int.
Is LinkedList size() slow? No. The list stores its size in a field, so size() takes constant time however many elements there are.
What is the difference between size() and length? size() is a method on collections such as LinkedList. length is a field of arrays, and length() is a method of String.
What is the last valid index of a LinkedList? size() - 1. An index equal to size() is out of range for get and set.
Does size() count null elements? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion