I'm looking for a clean and efficient way to apply a consumer to one element of a non parallel stream without closing the stream.
I mean I want to replace
AtomicBoolean firstOneDone = new AtomicBoolean();
lines.forEach(line -> {
if (!firstOneDone.get()) {
// handle first line
firstOneDone.set(true);
} else {
// handle any other line
}
})
with something alike
lines.forFirst(header -> {
// handle first line
}).forEach(line -> {
// handle any other line
})
I don't want to do two passes over the whole stream (copy or recreate the stream, peek
, etc.), or just move the boolean/test to another location like a function wrapper.
Is that possible or is the fundamental model of streams not compatible with this kind of partial reading ?