Java Collections indexOfSubList() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Collections

Definition

In Java, Collections.indexOfSubList(source, target) searches source for the first place where all elements of target appear next to each other, in the same order. It returns the index in source where that run starts, or -1 if there is no such run.

Think of it as String.indexOf for lists: indexOf finds one element, while indexOfSubList finds a whole sequence such as [L, L, R].

A static method of the java.util.Collections utility class, available since Java 1.4. Call it on the class name, as in Collections.indexOfSubList(...), after import java.util.Collections;. You never create a Collections object.

Syntax

Pass the list to search in, then the sequence to look for.

Java
List<Integer> data = Arrays.asList(1, 2, 3, 2, 3);
int i = Collections.indexOfSubList(data, Arrays.asList(2, 3));   // 1
int j = Collections.indexOfSubList(data, Arrays.asList(3, 1));   // -1
CallWhat it does
Collections.indexOfSubList(src, target)First start index, or -1.
Collections.lastIndexOfSubList(src, target)Last start index, or -1.
Collections.indexOfSubList(src, emptyList)Returns 0: an empty run "appears" at the start.
src.indexOf(x)Finds a single element.

Elements are compared with equals (null matches null). The simple method tries each start position in turn, so in the worst case it takes about source.size() * target.size() comparisons.

Library declarations
Java
// java.util.Collections
public static int indexOfSubList(List<?> source, List<?> target)

Parameters

ParameterDescription
sourceThe list to search in.
targetThe sequence to search for.

Return Value

Return ValueDescription
intThe index in source where the first matching run begins.
-1No matching run exists, for example when target is longer than source.

Compare with Similar Methods

Related search methods:

MethodHow it differs
lastIndexOfSubListSame search, but returns the last match.
list.indexOf(o)Looks for one element, not a sequence.
binarySearchFast single-element search in a sorted list.
disjoint(a, b)Checks for any shared element, ignoring order and position.

Java Examples

Example 1 — Find a Dance Step Pattern

Look for the first L, L, R in a list of steps, and for a pattern that never occurs.

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

public class CollectionsIndexOfSubListSteps {
    public static void main(String[] args) {
        List<String> steps = Arrays.asList("L", "R", "L", "L", "R", "L", "L", "R");

        int llr = Collections.indexOfSubList(steps, Arrays.asList("L", "L", "R"));
        int rr = Collections.indexOfSubList(steps, Arrays.asList("R", "R"));

        System.out.println("L L R first starts at " + llr);
        System.out.println("R R starts at " + rr);
    }
}

How It Works

  1. Try each start. At index 0 the run is L, R (mismatch on the second step); at index 1 it starts with R (mismatch at once).
  2. Match at 2. Indexes 2, 3, 4 hold L, L, R, so the method stops and returns 2. A second match at 5 is never reached.
  3. No R R. Every R in the list is followed by an L or is last, so the search returns -1.

Example 2 — Numbers, an Empty Target, and a Too-Long Target

See what happens with a normal match, an empty pattern, and a pattern longer than the list.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsIndexOfSubListEdges {
    public static void main(String[] args) {
        List<Integer> nums = Arrays.asList(4, 8, 15, 16, 23, 42);

        System.out.println(Collections.indexOfSubList(nums, Arrays.asList(15, 16)));
        System.out.println(Collections.indexOfSubList(nums, new ArrayList<Integer>()));

        List<Integer> longer = new ArrayList<>(nums);
        longer.add(99);
        System.out.println(Collections.indexOfSubList(nums, longer));
    }
}

How It Works

  1. Normal match. 15 and 16 sit side by side at indexes 2 and 3, so the answer is 2.
  2. Empty pattern. There is nothing to compare, so the very first start position, 0, already counts as a match.
  3. Cannot fit. The target has seven elements and the source only six, so no start position is possible and the result is -1.

Example 3 — Find a Phrase and Replace It

Locate the two-word phrase "bad word" in a token list and replace it with a single mask token.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsIndexOfSubListCensor {
    public static void main(String[] args) {
        List<String> tokens = new ArrayList<>(Arrays.asList("this", "is", "a", "bad", "word", "test"));
        List<String> phrase = Arrays.asList("bad", "word");

        int at = Collections.indexOfSubList(tokens, phrase);
        if (at >= 0) {
            tokens.subList(at, at + phrase.size()).clear();
            tokens.add(at, "***");
        }
        System.out.println("Found at " + at + ": " + tokens);
    }
}

How It Works

  1. Locate the phrase. bad and word are at indexes 3 and 4, so indexOfSubList returns 3.
  2. Remove the run. tokens.subList(3, 5).clear() deletes exactly those two tokens from the backing list.
  3. Insert the mask. tokens.add(3, "***") puts one token where the phrase began, so "test" follows it.

Frequently Asked Questions

The index in the source list where the first occurrence of the target sequence starts, or -1 if it does not occur.
Yes. The target must appear as a continuous run in the same order. Elements spread out across the list do not count.
0, because an empty sequence matches at the very first position.
With equals. A null element in the target matches a null element in the source.
Use Collections.lastIndexOfSubList(source, target).

Next: lastIndexOfSubList()

Find where a sequence of elements last appears in a list.

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

7 people found this page helpful