Check out a free preview of the full Java Fundamentals course:
The "Streams Overview" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie discusses streams, a sequence of elements from a data source, that can be accessed by using the package. An overview of intermediate and terminal stream operations is also covered in this segment.

Get Unlimited Access Now

Transcript from the "Streams Overview" Lesson

>> In addition to the java.util function package, Java also provides us with a package for your functional programming needs. So a stream represents a sequence of elements from a collection, from an array, or some other input source. So the way that it works, it does not store elements like collections do.

[00:00:28] Instead, it allows you to perform operations on the data without affecting the original data source, right? So it contains built-in methods and classes and interfaces that allow you to perform some operations. So given that you have an original source, you could then call any of the stream operations.

[00:00:54] And it could do some sort of processing of the data without affecting the original source. So the original source stays intact and you can output what would happen, blah, blah, blah, I've processed this in this way, here's the output. So here's an array of numbers. So we could create a stream of this array simply by calling string.

[00:01:23] So this is on the arrays class. This would be a static method on arrays that says, okay, turn, take that data source and I need to make a stream out of it. Given the stream, I then can do a dot on that stream to perform any operations that I want, so I can use forEach, for example.

[00:01:46] Now, on the numbers array by itself, I wouldn't have access to this forEach method, so this is a great way to have the forRach on arrays. You would turn it into a stream, say forEach, and then do whatever you want. So in this case, this is gonna print out the number +1.

[00:02:07] So that would be the output. Now, that's the output, right? I have not modified the numbers array at all. That array still stays intact the way that it is, but I performed some operation on the data that was there, and then produced the output based on that. Right, so the stream API also provides a method to process operations on the elements in parallel.

[00:02:37] So I can do a couple of different operations all at the same time. So if I did this, I'm basically saying in parallel, I want you to run forEach. So it's just kinda doing it kinda randomly. And notice the numbers are not in any sort of order or anything, because they all were kinda happening at the same time.

[00:03:04] Now, it's going to use multiple threads to process the elements at the same time. So again, it's not gonna be sequential. Every time you run this, it's likely to be different, or whatever. So if you cared about ordering and stuff, this is not a great use. If you don't care, you just need everything done, right, then this would be effective.

[00:03:27] So the stream API provides several useful operations that can be performed on its trees, and these operations can be either intermediate or they are terminal. For intermediate operations, it's gonna return the resulting stream after it's finished what it's doing. And this will enable you to continue running even more operations on that resulting stream.

[00:03:55] Again, the original data source wasn't affected, right? So the stream has all of that data processed. It could say, here's that stream, the resulting stream back to you in case you wanna do some more processing on it. Or with the terminal one, it closes the stream. It means this is the final operation that can be performed on that stream, and the stream is closed.