I want to check the order of two elements in a Java-8-Stream. I have this Iterator-Solution:
public static boolean isBefore (A a1,
A a2,
Stream<A> as) {
Iterator<A> it = as.iterator ();
while (it.hasNext ()) {
A a = it.next ();
if (a == a1) {
return true;
} else if (a == a2) {
return false;
}
}
throw new IllegalArgumentException (
a1 + " and + " a2 + " arent elements of the stream!");
}
Is there a solution without iterators in a more functional/non imperative style?
It could be something like this
as.filter (a -> (a==a1) || (a==a2)).findFirst ()
And then check if the result is a1 or a2 or empty. I'm sorry I don't give a full solution, it's hard to do if from my smart phone
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.