我在 groovy 中阅读 how to check if a list contains a sublist - stackoverflow .
如果有一种方法可以检查列表是否包含子列表,但以给定的顺序,我很感兴趣。例如,此代码将给出 true,
List<String> list = Arrays.asList("PRP", "VBP", "VBN", "NN", "NNS", "MD", "VB");
List<String> sublist = Arrays.asList("MD", "VB", "VBN");
System.out.println(list.containsAll(sublist));
但我想找回false。
最佳答案
您可以使用方法Collections.indexOfSubList
.
Returns the starting position of the first occurrence of the specified target list within the specified source list, or
-1
if there is no such occurrence. More formally, returns the lowest index i such thatsource.subList(i, i+target.size()).equals(target)
, or-1
if there is no such index. (Returns-1
iftarget.size()
>source.size()
.)
int index=Collections.indexOfSubList(list , sublist);
简短内容:
如果 Collections.indexOfSubList(list , sublist) =! -1
你会有一场比赛
关于java - 如何检查列表是否包含 Java 中给定顺序的子列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32864977/