Playing practically with Stream API
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.
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 namesProblem 2:
Find out 3 products with minimum ratings.
Sort products by (i) product ratings.
If 2 products have same amount of ratings, sort by namesWell, this is a test to solve for the readers.
Problem 3:
Find out total orders of each specific kind of productTo 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 productProblem 5:
Calculate the total cost of a order for a customer (customer 1, 2nd order)Problem 6:
Find out all books written by each authorThe full code in one file can be found here.
Acknowlegdement:
- A Big thanks to my respected senior brother, A N M Bazlur Rahman vaiya, for reviewing and sharing different ideas to solve the problems.
- Thanks to Shams Bin Sohrab vai for reviewing the codes.
- Cover image is taken from Telusko










