Home > database >  Explanation of How Java Stream Works
Explanation of How Java Stream Works

Time:11-15

I have a definition of a Stream such as: "They are wrappers around a data source, allowing us to operate with that data source and making bulk processing convenient and fast. "

Can someone give an example and just a basic explanation of how it works such that Stream makes "bulk processing convenient and fast"?

Thank you!

CodePudding user response:

Files.newBufferedReader("/tmp/foo").lines().map(...)...collect(...);
// or
BufferedReader reader = Files.newBufferedReader("/tmp/foo");
Stream<String> stream = reader.lines();
Collection<String> result = stream.map(...)...collect(...);

Is a convenient way to process a text file using a Stream.

But the work of making it fast/efficient is being done by the BufferedReader, not the Stream.

  • Related