You mean your original code versus mine? I would say that if you want to operate on individual lines, my example conveys the intention more clearly. Your code is more flexible though, as it allows you to tokenize the input in any way you want (consider Scanner::findWithinHorizon). I'm not sure if that's really helpful for keyboard input though.
I don't really like using
generate() without a
limit() or
find...() operation, because
generate() and
iterate() are supposed to return infinite streams. If I'm honest, I don't like using streams for input in the first place, because lazy evaluation is unreliable if the underlying data source is not referentially transparent. Another problem with
generate() is that it produces an unordered stream, which means that if the user enters multiple lines, operations performed on late user input may be executed before operations on early user input.
I feel Streams in
Java are like inheritance and operator overloading: Awesome features, but when we first learn about them, we tend to overuse them. My advice is to stick to imperative programming when you have a 'sequential' flow in your application, like handling user input. Streams are awesome for processing *real* collections.