I'm looking for a lambda to refine the data already retrieved. I have a raw resultset, if the user do not change the date I want use java's lambda to group by the results for then. And I'm new to lambdas with java.
The lambda I'm looking for works simliar to this query.
select z, w, min(x), max(x), avg(x), min(y), max(y), avg(y) from table group by x, w;
So I'm assuming you have a List of objects and you want to create a map with the given groupings. I am a bit confused by your x, y, w, z so I'll use my own fields. But Here's how I would do it:
Then if you want to get, say, the average of data for items with groups A, B then you use:
If you want to summarise more than one set of data simultaneously then it gets a bit more complicated. You need to write your own wrapper class that can accumulate multiple statistics. Here's an example with both data items in Entry (I made them an int and a double to make it a bit more interesting).
This class can then be used to create your own collector:
Now your maps return a CompoundStats instead of an IntSummaryStatistics:
Also note that this would be neater if you created a separate class to hold your groupings rather than using the two step map I've proposed above. Again not a difficult modification if required.
Hopefully this is useful in your own case.
I'm going to be using the
Tuple2
type from jOOλ for this exercise, but you can also create your own tuple type if you want to avoid the dependency.I'm also assuming you're using this to represent your data:
You can now write:
Or even better (using the latest jOOλ API):
The map structure is now:
Calling
toString()
on the above map will produce:You got all your statistics now.
Side note
Of course, I don't know your exact requirements, but I suspect you'll be quickly needing more sophisticated aggregations in your report, such as medians, inverse distribution, and all sorts of nice OLAP features, which is when you realise that SQL is just a much easier language for this kind of task.
On the other hand, we'll definitely add more SQLesque features to jOOλ. This topic has also inspired me to write a full blog post with more details about the described approach.