Frequency, Sum, Average and Map Reduce with java 8


August 8, 2014 . Comments
Tags: java, Agregation, java 8

 
 
  

Problem

How can we do group by and get the count of occurrences of objects in a list. We want to do that based on certain attributes only.

The Object we will be using for holding the value will be Product

We will be using an ArrayList to hold a collection of products. I would like to know the number of products being sold for each currency.

Solution

If you want to do the same code using stream in java 8 the below is the way to go. But good luck debugging through this. The map reduce like api's introduced in java 8 are really cool but is something i have been using in C#.net since 3.5. Both have the same problem.

comparing performance of both the above implementation the stream implementation is slightly lower in performance when the list size grows higher but can be easily parallelized.

If you want to get a sum of all the product quantity grouped by currency then the below code will work

If you want to get the average of the product quantity based on Currency the below code will do.

Now how can we get the list filtered based on currency "USD".

The map reduce based aggregation operations in java is very powerful. Until now we have only seen the simplicity at which we can write map reduce like operations using java 8 this is all pretty code to me and saving the number of lines to write. Performance is the same either you use the Aggregation API or do it your self using plain HashMap or Arrays.

When we can do any of the above operations in parallel using all the available CPU's for a single map-reduce then the power shows up. Let us take the avg example and try to do it with parallel stream

Yup plain and simple just adding parallelStream gives the solution. Please drop a comment if you like this blog.



Comments Section

Feel free to comment on the post but keep it clean and on topic.