Java LinkedList subList() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, subList(fromIndex, toIndex) returns a List that shows the part of the LinkedList from fromIndex (included) up to toIndex (not included).

The result is a view, not a copy. Reading it reads the original, and changes made through the view, such as set or clear(), change the original list. To get an independent list, copy it with new LinkedList<>(list.subList(a, b)).

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

Give the start index and the end index (not included).

Java
LinkedList<String> list = new LinkedList<>(Arrays.asList("A", "B", "C", "D", "E"));
List<String> part = list.subList(1, 4);          // [B, C, D]
part.clear();                                     // the original is now [A, E]
List<String> copy = new LinkedList<>(list.subList(0, 1)); // independent copy
CallWhat it does
list.subList(from, to)A view of the elements from from up to, but not including, to.
list.subList(from, to).clear()Removes that range from the original list.
new LinkedList<>(list.subList(from, to))Makes an independent copy of the range.
list.subList(0, n)The first n elements.

The size of the view is toIndex - fromIndex. An empty range such as subList(2, 2) is allowed.

Library declarations
Java
// java.util.LinkedList (inherited from AbstractList)
public List<E> subList(int fromIndex, int toIndex)

Parameters

ParameterDescription
fromIndex (int)The first index of the range (included).
toIndex (int)The end of the range (not included). Can be as large as size().

Return Value

Return ValueDescription
List<E>A view of the range. It is backed by the original list.

Compare with Similar Methods

Ways to take part of a list:

MethodHow it differs
clear()Called on the view, it removes just the range.
toArray()Copies all the elements into an array.
removeIf(filter)Removes by a rule instead of by a range.
ArrayList.subList()The same method on an array-backed list.

Java Examples

Example 1 — Take a Range of a List

Print a middle range and the first three elements, and check the size of the view.

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

public class LinkedListSubListDemo {
    public static void main(String[] args) {
        LinkedList<String> months = new LinkedList<>(
            Arrays.asList("Jan", "Feb", "Mar", "Apr", "May", "Jun"));

        List<String> spring = months.subList(2, 5);
        System.out.println("range 2 to 5: " + spring);
        System.out.println("view size: " + spring.size());
        System.out.println("first three: " + months.subList(0, 3));
        System.out.println("empty range: " + months.subList(4, 4));
    }
}

How It Works

  1. The end index is excluded. subList(2, 5) covers indexes 2, 3 and 4, which are Mar, Apr and May.
  2. The view size. It is 5 minus 2, so it holds 3 elements.
  3. Starting at 0. subList(0, 3) gives the first three months.
  4. An empty range is legal. When both indexes are equal, the view holds no elements.

Example 2 — Change a List Through Its subList

Replace and clear elements through the view and see the original list change.

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

public class LinkedListSubListView {
    public static void main(String[] args) {
        LinkedList<Integer> nums = new LinkedList<>(Arrays.asList(1, 2, 3, 4, 5, 6));

        List<Integer> middle = nums.subList(1, 5);
        middle.set(0, 20);
        System.out.println("after set: " + nums);

        middle.clear();
        System.out.println("after clear: " + nums);
        System.out.println("view is now: " + middle);
    }
}

How It Works

  1. A window on indexes 1 to 4. The view holds 2, 3, 4 and 5.
  2. set writes through. The first element of the view is the second element of the original, so nums now has 20 in that place.
  3. clear removes the range. All four elements of the view are deleted from nums, leaving the first and last numbers.
  4. The view stays in sync. It is empty now, because its elements are gone from the original.

Example 3 — Make an Independent Copy and Meet the Exceptions

Copy a range into a new list, then try bad ranges and use an old view after the list changed.

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

public class LinkedListSubListErrors {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>(Arrays.asList("a", "b", "c", "d"));

        List<String> copy = new LinkedList<>(list.subList(1, 3));
        List<String> view = list.subList(1, 3);
        list.add("e");
        System.out.println("copy after add: " + copy);

        try {
            System.out.println(view.size());
        } catch (RuntimeException e) {
            System.out.println("old view -> " + e.getClass().getSimpleName());
        }

        try {
            list.subList(0, 9);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("subList(0, 9) -> IndexOutOfBoundsException");
        }

        try {
            list.subList(3, 1);
        } catch (IllegalArgumentException e) {
            System.out.println("subList(3, 1) -> IllegalArgumentException");
        }
    }
}

How It Works

  1. A copy is independent. copy is a brand-new list holding b and c, so adding "e" to the original does not affect it.
  2. An old view breaks. The original changed its size after view was created, so using the view throws ConcurrentModificationException.
  3. An end beyond the size. subList(0, 9) asks for more elements than exist, so it throws IndexOutOfBoundsException.
  4. A backward range. subList(3, 1) starts after it ends, which is an IllegalArgumentException.

Frequently Asked Questions

A view of the elements from fromIndex (included) to toIndex (not included). It is backed by the original list, not a copy.
Wrap the view in a new list: new LinkedList<>(list.subList(from, to)).
Call list.subList(from, to).clear(). It removes those elements from the original list.
The original list was changed (elements added or removed) after the view was created. Create a fresh view after such changes.
No. The range runs from fromIndex up to, but not including, toIndex.

Next: toArray()

Copy the elements of the list into an array.

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

13 people found this page helpful