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 such source.sublist(i, i+target.size()).equals(target), or -1 if there no such index. (returns -1 if target.size() > source.size().)

int index=collections.indexofsublist(list , sublist); 

short:
if collections.indexofsublist(list , sublist) =! -1 have match


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -