How to check if a list contains a sublist in a given order in Java -
i read in groovy how check if list contains sublist - stackoverflow .
i interested if there way of checking whether list contains sublist, in given order. example, code give 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));
but want false back.
you can use method collections.indexofsublist
.
returns starting position of first occurrence of specified target list within specified source list, or
-1
if there no such occurrence. more formally, returns lowest index suchsource.sublist(i, i+target.size()).equals(target)
, or-1
if there no such index. (returns-1
iftarget.size()
>source.size()
.)
int index=collections.indexofsublist(list , sublist);
short:
if collections.indexofsublist(list , sublist) =! -1
have match
Comments
Post a Comment