I'm using code like this to process a file consisting of multiple lines:
BufferedReader reader = ...
reader.lines().forEach(Same common Action)
This works fine as long as every line needs to be handled the same way. But sometimes there may be several different actions.
For example, let's say the first line is a header and the other lines are content. For the first line I want to perform Action1, and for the others I want Action2.
In Java 7 style I'd do something like this:
String line;
boolean first = true;
while ( (line = reader.readLine()) != null) {
if (first) {
action1(line);
first = false;
} else {
action2(line);
}
}
But that's complex and ugly, and it is not using streams at all. How can I do that in an idiomatic way using Java 8 streams?