Java Collections lastIndexOfSubList() Method

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

Definition

In Java, Collections.lastIndexOfSubList(source, target) finds the last place in source where the elements of target appear next to each other in the same order. It returns the index where that last run starts, or -1 if the run never appears.

It is the list version of String.lastIndexOf. Use it when the most recent occurrence matters, such as the last time a pair of events happened in a log.

A static method of the java.util.Collections utility class, available since Java 1.4. Call it on the class name, as in Collections.lastIndexOfSubList(...), 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, 1, 2, 3);
int last = Collections.lastIndexOfSubList(data, Arrays.asList(1, 2));    // 3
int none = Collections.lastIndexOfSubList(data, Arrays.asList(3, 3));    // -1
CallWhat it does
Collections.lastIndexOfSubList(src, target)Last start index, or -1.
Collections.indexOfSubList(src, target)First start index, or -1.
Collections.lastIndexOfSubList(src, emptyList)Returns src.size().
src.lastIndexOf(x)Last position of a single element.

The search starts with the latest start position that still leaves room for the whole target (source.size() - target.size()) and moves toward the front, so the first match it finds is the last one in the list.

Library declarations
Java
// java.util.Collections
public static int lastIndexOfSubList(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 last matching run begins.
-1The target does not occur in source.

Compare with Similar Methods

Related search methods:

MethodHow it differs
indexOfSubListSame search from the front; returns the first match.
list.lastIndexOf(o)Last position of one element.
frequency(c, o)Counts single elements instead of locating runs.
binarySearchSingle-element search in sorted data.

Java Examples

Example 1 — Find the Last WARN Followed by ERROR

In a list of log levels, compare where the first and the last WARN, ERROR pair begins.

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

public class CollectionsLastIndexOfSubListLog {
    public static void main(String[] args) {
        List<String> log = Arrays.asList("INFO", "WARN", "ERROR", "INFO", "WARN", "ERROR", "INFO");
        List<String> pair = Arrays.asList("WARN", "ERROR");

        System.out.println("first: " + Collections.indexOfSubList(log, pair));
        System.out.println("last:  " + Collections.lastIndexOfSubList(log, pair));
    }
}

How It Works

  1. Two occurrences. WARN, ERROR appears at indexes 1-2 and again at 4-5.
  2. Search from the back. lastIndexOfSubList starts at index 5 (7 minus 2), finds ERROR, INFO there, then tries index 4 and matches.
  3. Side by side. Printing both results shows the difference: 1 for the earliest run and 4 for the latest.

Example 2 — Overlapping Matches and an Empty Target

Search a run of identical numbers, then search for an empty list.

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

public class CollectionsLastIndexOfSubListOverlap {
    public static void main(String[] args) {
        List<Integer> ones = Arrays.asList(1, 1, 1, 1);
        List<Integer> pair = Arrays.asList(1, 1);

        System.out.println("first [1, 1]: " + Collections.indexOfSubList(ones, pair));
        System.out.println("last [1, 1]:  " + Collections.lastIndexOfSubList(ones, pair));
        System.out.println("last []:      " + Collections.lastIndexOfSubList(ones, Collections.<Integer>emptyList()));
    }
}

How It Works

  1. Overlaps count. The pair matches at starts 0, 1, and 2, because runs are allowed to share elements.
  2. Last valid start. A pair needs two slots, so index 2 is the latest place it can begin; index 3 would run off the end.
  3. Empty target. With nothing to match, the very latest start position, ones.size() (4), is returned, just like "abcd".lastIndexOf("") returns 4.

Example 3 — Keep Everything Up to the Last Phrase

Cut a token list right after the last occurrence of "the end".

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

public class CollectionsLastIndexOfSubListTrim {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("the", "end", "is", "not", "the", "end", "really");
        List<String> marker = Arrays.asList("the", "end");

        int at = Collections.lastIndexOfSubList(words, marker);
        List<String> kept = new ArrayList<>(words.subList(0, at + marker.size()));

        System.out.println("Last marker at " + at);
        System.out.println("Kept: " + kept);
    }
}

How It Works

  1. Two markers. "the end" occurs at index 0 and at index 4; the method reports 4.
  2. End of the marker. Adding the marker length (2) gives 6, the index just after the last "end".
  3. Independent copy. subList(0, 6) is copied into a new ArrayList, dropping only the trailing "really".

Frequently Asked Questions

The index where the last occurrence of the target sequence starts in the source list, or -1 if it does not occur.
indexOfSubList returns the first matching start index; lastIndexOfSubList returns the last one.
Yes. In [1, 1, 1, 1], the sequence [1, 1] matches at 0, 1, and 2, and the last one is 2.
The size of the source list, which is the latest position where an empty sequence can start.
No. It only reads both lists.

Next: list()

Copy the elements of an Enumeration into a new ArrayList.

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

8 people found this page helpful