Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.3 Understanding Streams

With the Stream API, Java 8 offers a radically different approach to handling sequences of data. In this lesson, you'll learn how to use streams to perform simple operations.

2.3 Understanding Streams

Welcome back. In this lesson we are going to take a look at a very useful API that was introduced in Java 8. It's called the Streams API, and it offers a whole new approach to working with arrays and collections. So I've gone ahead and created a new activity for this lesson, and I suggest that you do the same if you're coding with me. So before we get started with Stream API let us quickly create a new array of integers. And add some integers to it. Java 8 allows you to convert any array or list into a stream. For example, if you want to convert this array to a stream, all you need to do is pass it to the Arrays.stream method. This method returns an IntStream object. Before we do something with our stream, let's create another stream, this time, let's create a stream from a list. So first create a list, this can be list of strings. And then add some strings to the list by calling it add method. I'm going to add three strings. All right, now to convert this list into a stream, all you need to do is call the stream method again. This time, the stream method is available in the list class itself. The method returns a stream of type string. So, for some perimeters such as int, double, and long, there are distinct string classes. For all other types, there's this generic stream class. There's not much of a difference between the two. But, IntStream has a few extra methods that can be quite handy. For example, if we want to create a stream of all integers that fall within a certain range, we could simply use the IntStream.range method, and specify the range We have three streams now, so it's time to do something useful with them. Let's start by trying to print all the contents of the stream. One important thing you need to understand now, is that, you generally don't use conventional loops with streams. So an ordinary for loop or a while loop, shouldn't be used now. Those loops are far arrays and collections only, not streams. If you really want to use a for a loop, you'll have to convert the stream back to an array by calling the two array function. But we won't be doing that right now. To perform operations on strings, you must use lambdas and follow, a functional programming-based approach. So the easiest way to loop through all elements of a stream, is to use the forEach method. As you can see, this expects an intConsumer, which is just a functional interface with a method, that accepts an integer as it's input. So we can create a lambda here that looks like this. This x is of course an integer. With forEach method, this lambda is going to receive every single element that's present in the stream one at a time. That means we can simply print the value of x here. And be sure that the value of every element of the stream gets printed. If you're on the app now, you should be able to see this output. We can write a similar loop for the stream of strings too. In this case, the x will be a string. Let's try something different now. Let's say you want to double the value of all the numbers in the range stream, and then print them. A naive approach to do that would be to double the value inside the forEach loop itself before printing it. But, a better approach would be to use the map method. This method allows you to modify the elements of your stream. So it takes each element of the stream, one at a time, as its input and then returns a new element. In this case, we want to return 2 times x. This is a single statement lambda, so you can convert it to an expression lambda if you want to. At this point, we will have a new stream containing the doubled elements. That means we can use the for each method of this new stream and simply print it's elements. What we just did was create a chain of streams, a chain of two streams to be more precise. We started with the original stream, converted it into another stream using the map method, and finally printed the contents of the new stream. Let's run our app again to see if we have the right results. Looks like we do. All right, that brings us to the end of this lesson. In the next lesson, we are going to do more advanced stream operations, and witness the real power of streams, thanks for watching.

Back to the top