Reputation: 2807
I know that we can use the containsAll
method while comparing two ArrayLists
to check if one is a subset of the other. But this is what I would like. Consider an
ArrayList super = 1,2,3,4,5,6,7,8,9,10
and an
ArrayList sub1 = 1,2,3,4
and another
ArrayList sub2 = 2,4,6,8
.
Now, if I did
super.containsAll(sub1)
, it would evaluate to true
because sub1
is contained within super
.
If I did super.containsAll(sub2)
, it would also evaluate to true
because the numbers 2,4,6 and 8 are contained in super
.
I would like to know if there's a way to check two ArrayLists
so that super.containsAll(sub2)
evaluates to false
as the numbers in sub2
don't appear in the same order in super
.
Upvotes: 4
Views: 3627
Reputation: 6517
I believe you can use Collections.indexOfSublist
to do that. More info here: http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html
Upvotes: 7
Reputation: 2168
I guess you have to implement it yourself. You should traverse super, and if super(i).equals(sub2(0))
, check that the next coming items are identical to those of sub2.
Upvotes: 0