EC by Example: AggregateBy in MapIterable to aggregate on key and value

Alex Goldberg
2 min readJul 30, 2020

--

I have recently contributed to Eclipse Collections project implementing aggregateBy variation to aggregate on key and value.

How does it work?

Imagine you have a Map of Intervals. Your keys are human readable descriptions of the intervals (for example “oneToFive”, “sixToNine”, “tenToTwenty”). Your values are the intervals themselves.

What if you had to sum the values of all the intervals up and aggregate them based on different keys? What if instead of “oneToFive”, “sixToNine”, “tenToTwenty” you had to aggregate to “lessThanTen” and “greaterOrEqualsToTen”?

The new aggregateByvariation makes it very easy to implement.

What are some other use cases?

AggregateBy can come in handy if we have a Map of pets with keys as tuples of pet type and gender and values as pet ages and want to aggregate up to the pet type or gender. We can easily answer queries such as what is the number of male pets of the ages greater or equal to n or what is the total number of dogs or cats of some age criteria.

Another use case could be aggregating up a Map of trade attributes to trade balances. Queries such as what is the total sum of certain balances for a subset of trade attributes could be easily answered.

The new aggregateBy methods will be available in the next release of EC (10.3.0).

For more information about Eclipse Collections check out the repo on GitHub.

--

--