Java 25 (LTS) reference · List and ArrayList Methods

Java List subList() Method

Return a view of part of a list, from an index up to (not including) another. Changes to the view affect the original.

Java 25 (LTS) Since Java 1.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

list.subList(fromIndex, toIndex)

Parameters

ParameterDescription
fromIndex
int
First index (inclusive).
toIndex
int
End index (exclusive).

Return value

A List<E> view.

Examples

A slice of a list
import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> nums = List.of(10, 20, 30, 40, 50);
        System.out.println(nums.subList(1, 4));
        System.out.println(nums.subList(0, Math.min(3, nums.size())));
    }
}

Output

[20, 30, 40]
[10, 20, 30]

Run this on your computer — see “Get Started” for setup.

Views write through
import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<String> items = new ArrayList<>(List.of("a", "b", "c", "d"));
        items.subList(1, 3).clear();   // removes b and c from the original
        System.out.println(items);
    }
}

Output

[a, d]

Run this on your computer — see “Get Started” for setup.

Version history

VersionChange
Java Java 1.0Added

Was this page helpful?