Issue
I have following code:
Stream<String> lines = reader.lines();
If fist string equals "email"
I want to remove first string from the Stream. For other strings from the stream I don't need this check.
P.S.
Sure I can transform it to the list, then use old school for loop but further I need stream again.
Solution
While the reader will be in an unspecified state after you constructed a stream of lines from it, it is in a well defined state before you do it.
So you can do
String firstLine = reader.readLine();
Stream<String> lines = reader.lines();
if(firstLine != null && !"email".equals(firstLine))
lines = Stream.concat(Stream.of(firstLine), lines);
Which is the cleanest solution in my opinion. Note that this is not the same as Java 9’s dropWhile
, which would drop more than one line if they match.
Answered By - Holger
Answer Checked By - Robin (JavaFixing Admin)