Getting started with Java Streams


Introduced in JDK v1.8, Streams concepts makes the life of  programmers a lot easier. Don’t believe me! read along, at the end of this blog you’ll have a basic idea how we can use Streams APIs in our day to day programming life and once you get the hang of it, you will love it.

Now, I know you will be thinking about what’s this Stream concept and how and where I can use it and how it will make my life easier? Well, we’ll get to that soon, but first let’s have a look at the prerequisite.

Prerequisite

You should have  knowledge of Java Collection and Lambda expressions and you’ll also need JDK v1.8.

Where can one use Streams APIs and what is it?

Now, we know if we want to represent a group of objects as a single entity then we go for collection. Let’s say, you have a collection and you want to perform some operation on each element of the collection then you should prefer Streams APIs. 

Let’s understand this with a program written to get all the even numbers present inside a collection.

The above example demonstrated how we usually separate the even numbers from a collection by iterating the loop and performing some operations to it. Now let’s see how with stream APIs we can achieve the same goal.

The code looks less messy right!  Let’s dissect the code and understand what we did in the above example to get all the even numbers in the lObEvenNumberList.

lObjRandomNumbers.stream()

To call any Streams API we need an Instance of it. So we can call .stream() on any collection object. It will basically give us an instance of Streams class, later we can call any APIs to perform certain operations. Like

lObjRandomNumbers.stream().filter(i -> i%2 == 0)

filter() method of the Streams takes a Lambda expression which is basically telling that for each element present in the collection do find its reminder after it’s been divided by 2 and check if reminder  is equal to 0 if yes then filter those elements, and once filtered we call .collect() to collect those elements. Here is the code for that

lObjRandomNumbers.stream().filter(i->i%2==0)

.collect(Collectors.toList());

Wasn’t this easy we just had to write one line of code and boom we have the result with us.

Now let’s take another example. Let’s say we want to multiply each element present in the above Collection by 5. Here is the code for that with Streams APIs

List<Integer> lObjUpdatedList = 

lObjRandomNumbers.stream().map(i -> i*5).collect(Collectors.toList());

Similarly, here we have used a Lambda expression which tells us to multiply each element present inside the collection by 5 and collect the data in the form of a list.

From the above two examples I hope you have got the basic idea of how and where to use the Streams API. There are other Streams Apis available. I’ll strongly suggest having a look at those. Some of the other APIs are..

count(), sorted(), min(), max(), forEach(), toArray(), Stream.of()…

Leave A Comment

Your email address will not be published. Required fields are marked *