Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether a list is a subset of another list [duplicate]

list1:[1,2,3,4,5]
list2:[1,2,3]

How to check if list2 is a subset of list1? I tried containsAll() but it gives true as long as the elements in list 2 are present in list1. I want the same order as criteria and not just the elements.

like image 282
Zaq Avatar asked Dec 08 '22 03:12

Zaq


1 Answers

Use this:

boolean contains(List<?> list, List<?> sublist) {
    return Collections.indexOfSubList(list, sublist) != -1;
}

http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#indexOfSubList(java.util.List,%20java.util.List)

like image 103
Laszlo Hirdi Avatar answered Dec 11 '22 10:12

Laszlo Hirdi