indexOfSubList Locates A Nested Sequence
Find where a smaller list appears inside a larger one with Collections.indexOfSubList, matching consecutive element runs rather than single items like indexOf.
Code
import java.util.Collections;
import java.util.List;
public class PatternWindow {
public static void main(String[] args) {
List<String> events = List.of(
"boot", "login", "read", "write", "read", "write", "logout");
// indexOf 只找单个元素;indexOfSubList 匹配连续的元素序列
System.out.println("indexOf read: " + events.indexOf("read"));
System.out.println("read,write window: "
+ Collections.indexOfSubList(events, List.of("read", "write")));
// 从后往前找最后一次出现的起始下标
System.out.println("last read,write: "
+ Collections.lastIndexOfSubList(events, List.of("read", "write")));
// 空目标按正式规范匹配下标 0;找不到或目标比源长都返回 -1
System.out.println("empty pattern: "
+ Collections.indexOfSubList(events, List.of()));
System.out.println("missing run: "
+ Collections.indexOfSubList(events, List.of("write", "read", "write", "read")));
}
}How it works
The working surface is the class PatternWindow and the function main — everything else is wiring.
Caveats
- No external surface: all behavior comes from the standard library.
Putting it to use
The primary signature is public static void main(String[] args)
Output lines are prefixed (indexOf read:, read,write window:, last read,write:), making each step’s effect easy to spot.
Interactive Tools for This Topic
Fuzzy Match TesterTest subsequence fuzzy matching: check whether pattern characters appear in order inside a text, with positions and highlighting.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- What does indexOfSubList Locates A Nested Sequence print when run?
- Running the file directly is enough — expect labeled output lines such as `indexOf read:`, `read,write window:`, `last read,write:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.