Sitemap
Syed Hasan

Syed Hasan

Software Engineer | Back-End Developer | Spring Developer | Cloud Enthusiast

Playing practically with Stream API

2 min readFeb 14, 2022

--

Press enter or click to view image in full size

We can find many Stream API examples on Google, but most of them doesn’t represent the real life scenarios. Hence, it often remains black box to understand the power of the Stream API for many rising programmers.

In this article, I tried to make some scenario based problems where I have tried to make the solution with Stream API. These were initially for my personal practice purpose, and later, I added some more practice problems to demonstrate in my class at Pondit.

Get Syed Hasan’s stories in your inbox

Join Medium for free to get updates from this writer.

I have added the boilerplate codes of Model classes and initialization here.

Boilerplate codes and init method

Note: Perhaps, Cart would be more appropriate rather than naming the class as Order

Problem 1:

Find out 3 products with maximum ratings. 
Sort products by (i) product ratings.
If more than 1 products have same number of ratings, (ii) sort by names

Problem 2:

Find out 3 products with minimum ratings.
Sort products by (i) product ratings.
If 2 products have same amount of ratings, sort by names

Well, this is a test to solve for the readers.

Problem 3:

Find out total orders of each specific kind of product

To find out the total orders first we need to concat all orders by all customers.

Problem 4:

Find out customer 1's favorite product.
A favorite product is the maximum quantity a customer ordered of a specific product

Problem 5:

Calculate the total cost of a order for a customer (customer 1, 2nd order)

Problem 6:

Find out all books written by each author

The full code in one file can be found here.

Acknowlegdement:

  1. A Big thanks to my respected senior brother, A N M Bazlur Rahman vaiya, for reviewing and sharing different ideas to solve the problems.
  2. Thanks to Shams Bin Sohrab vai for reviewing the codes.
  3. Cover image is taken from Telusko

--

--

Syed Hasan
Syed Hasan