Intro to Java Streams

aditya goel
4 min readDec 26, 2022

Question #1.) What are Java Streams ?

Note: We are not responsible for performing the iteration.

Question #2.) Convert the below code to the Streams :

Solution → In the above code-piece, we are running a for loop and performing the business logic. The equivalent Streams code for the same shall be :-

Question #3.) Detail some important functions of Streams ?

Answer :- Following are the important functions available with Streams :-

  • map → This function allows us to substitute one element with another element in the stream.
  • filter → This function allows us to decide, whether particular element should continue to be in the stream or not ?
  • findFirst → This function allows us to find the first element, which matches the certain criteria.

Some examples with Stream of Numbers :-

Question #1.) Given the following stream of numbers :-

Here, our task is to print all the numbers into this stream ?

Answer :- This is how, we shall be able to print all the numbers :-

Approach no. 1 → Using the number to print the same. This approach is more obvious and easy to read :-

Approach no. 2 → Using the method reference :-

Question #2.) Given the following stream of numbers :-

Here, our task is to print only those numbers, which are lesser than 5 ?

Answer :- This is how, we shall be able to print all the numbers :-

Note that, we have used the proper naming convention in the above code-piece. We can use any other naming-convention as shown below :-

Question #3.) Given the following stream of numbers :-

Here, our task is to print the second and third numbers, which are greater than 5 ?

Answer :- This is how, we shall be able to print all the numbers :-

Question #4.) Given the following stream of numbers :-

Here, our task is to print the first number, which is greater than 5 and If nothing is found, then we end up printing -1 ?

Answer :- This is how, we shall be able to print all the numbers :-

The advantages with this type of coding with Streams are :-

  • We don’t have to keep track of the loop here.
  • We don’t have to keep track of the states somewhere.

Some examples with Stream of Objects :-

Question #1.) Given the following stream of Objects :-

Here, our task is to print the first name of all the users in the above stream.

Answer :- This is how, we shall be able to print all the required values:-

Note here that, map function replaces the value in the stream with the new value.

Question #2.) Given the following streams :-

  • Stream of Objects :-
  • Stream of Numbers :-

Here, our task is to print the first name of all the users from the userStream, for users that have IDs from the numberStream.

Answer :- This task is little complex, because we gotta join multiple streams here :-

  • Firstly, we are working on the stream of userObjects.
  • Next, we are filtering those users, whose id matches to the <list of id> from the another list of numberStream.
  • Finally, we are printing the userNames.

Thanks for reading through this blog. If you liked it, do clap on this page. We shall see you in next blog in this series.

--

--

aditya goel

Software Engineer for Big Data distributed systems