EC by Example: FlatCollect into primitive collections

Alex Goldberg
1 min readJun 18, 2020

--

I have recently contributed to Eclipse Collections project implementing flatCollectByte, flatCollectChar, flatCollectInt, flatCollectShort, flatCollectDouble, flatCollectFloat, flatCollectLong and flatCollectBoolean methods.

EC already had collectByte, collectChar and other primitive collect implementations. Adding primitive flatCollect implementations improved the symmetry between collect and flatCollect APIs in addition to adding useful functionality.

What is FlatCollect?

FlatCollect introduction by Donald Raab

This example is similar to the one in flat collect introduction referenced above. It demonstrates how to collect all primitive ints returned by oneTo() method of IntInterval into IntArrayList.

FlatCollectInt examples

Here is an example of using flatCollectInt to collect unique ages of all pets that belong to a list of people into IntSet.

Here’s another example of using FlatCollectInt to get counts by pet age by collecting into IntHashBag:

This is how getPetAges is implemented:

The new flatCollect methods will be available in the next release of EC (10.3).

For more information about Eclipse Collections, check out this presentation.

--

--