Tech Blog

Coding

Getting started with Java Streams

By Satyabrata Dash3 min read
← Back to blog

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()…

Keep reading

You may be interested in

Browse all articles →
Active Directory Federation Service
July 5, 20235 min read

Active Directory Federation Service

Active Directory Federation Service (ADFS) uses claims-based identity model to maintain application security and to implement federated identity. ADFS is a concept of Windows Server operating system, that provides users with single sign-on access to the application, those are located across organizational boundaries. For Example: We have 2 organizations – Organization 1 and Organization 2. […]

By Abinash MishraRead More →
Remote Access to Microsoft SQL Database Server
April 5, 20232 min read

Remote Access to Microsoft SQL Database Server

Remote access to a SQL server is important because it allows users to access the server from anywhere, at any time. This enables remote workers to access the data they need when they need it, even if they are not physically in the same location as the server. It also allows IT admins to access […]

By Malkeet SinghRead More →
Types of Load Balancer in AWS
February 6, 20235 min read

Types of Load Balancer in AWS

What is Load Balancing ? Load balancing is a critical component of any scalable and highly available Cloud or On-premise architecture. It helps distribute incoming traffic across multiple servers, improving the availability and scalability of a website or application. In the cloud, load balancing is offered as a service by various cloud providers such as […]

By Keerthik ShenoyRead More →