Real functional solutions, i.e. not containing a volatile state, hard to find. This is best illustrated by the fact that all answers still include mutable state.
In addition, there is no List.indexOf(T object, int startIndex) . To illustrate how useful this would be, define it using a helper method:
public static int indexOf(List<?> list, int startIndex, Object o) { if(startIndex!=0) list=list.subList(startIndex, list.size()); int ix=list.indexOf(o); return ix<0? -1: ix+startIndex; }
It would be easy to find an alternative implementation without a temporary object if this is a problem
Now a simple solution using mutable state would be:
boolean allMatch = sequence.stream().allMatch(new Predicate<Integer>() { int index = 0; public boolean test(Integer t) { return (index = indexOf(global, index, t)) >=0; } });
A functional solution without a mutable state requires that the value type contain two items in two lists. When we use the int[2] array for this, the solution will be:
boolean allMatch = Stream.iterate( new int[]{ 0, global.indexOf(sequence.get(0)) }, a -> new int[] { a[0]+1, indexOf(global, a[1], sequence.get(a[0]+1)) } ) .limit(sequence.size()) .allMatch(a -> a[1]>=0);
Holger
source share