从列表java中获取包含子列表的索引

R4j*_*R4j 5 java list

我有字符串列表看起来像这样:

 List<String> parentDataList:  {"this", "is", "a", "test", "string", "and", "a", "test", "other"} 
 List<String> child1:   {"a", "test"}
 List<String> child2:   {"this", "string"} 
 List<String> child3:   {"is", "a", "test"} 
Run Code Online (Sandbox Code Playgroud)

我的期望是我想检查父列表是否包含序列子列表,然后在子列表的父列表中获取开始和结束索引.
从上面的例子:

 Parent contain child1 list, and return the indexes: [2 - 3] and [6 - 7]
 Parent doesn't contain child2 list because it isn't sequential.
 Parent contain child3 list, and return the index: [1 - 3] 
Run Code Online (Sandbox Code Playgroud)

我尝试使用List.containsAll方法,但它不关心列表项的顺序,我不能从这个方法获得开始和结束索引.
我正在寻找最快的方法,因为我的列表有很多数据,我必须从许多输入字符串中搜索.
任何帮助,将不胜感激!

更新:
我需要获取子列表的所有索引都包含在父列表中.例如,父级包含两个位置的child1:[2 - 3]和[6 - 7]

Hol*_*ger 8

该方法Collections.indexOfSubList将为您提供所需的信息.

返回指定源列表中指定目标列表第一次出现的起始位置,如果不存在,则返回-1.更正式地,返回最低索引i,使得source.subList(i,i + target.size()).equals(target),或者如果没有这样的索引则返回-1.(如果target.size()> source.size(),则返回-1.)

int index=Collections.indexOfSubList(parentDataList, child1);
…
Run Code Online (Sandbox Code Playgroud)

索引间隔将从index(包括)到index+child1.size()独占.-1当然,除非返回索引.在后一种情况下,找不到子列表.