I'm trying to filter a sorted stream using java9´s takeWhile to get objects from the beginning of the stream that share the same value for a field. I am unable to write the right predicate to do so. This can be done using two steps breaking the pipeline of the stream.
Stream.of(objA, objB, objC, objD, objE, objF, objG)
.takeWhile(" get the value of objA´s field and take
as long as as other objects value for that field is the same as objA´s");
In two steps I could do something like
int x = Stream.of(objA, objB, objC, objD, objE, objF, objG).findFirst().get().getSomeValue();
Stream.of(objA, objB, objC, objD, objE, objF, objG).takeWhile(e -> e.getSomeValue() == x);
A simplified example could be
Stream.of(5,5,5,5,13,14,5,5,2,5,5,6)
.takeWhile(get the first four '5'´s)
Can this be done without the intermediate step using optional.get?