InjectIntoKeyValue in Eclipse Collections

Emilie Robichaud
2 min readApr 21, 2022

--

In the Eclipse Collections 11.1.0 release, a new API is being introduced! Recently, I worked on this issue to implement injectIntoKeyValue in primitive maps, as well as this issue for adding the method to object maps. This blog will focus on the functionality, examples, and implementation of this new API!

InjectIntoKeyValue — What Does It Do?

InjectIntoKeyValue implements the injectInto pattern on each key-value pair of a map. The method applies a three-argument function to each key-value pair of the map using an initial value and returns the result.

This description may be a bit vague, so I will further demonstrate it with a few examples.

Example: Sum

@Test
public void injectIntoKeyValueSum()
{
LongIntMap map = this.newWithKeysValues(1L, 2, 3L, 4);
Long sum = map.injectIntoKeyValue(new Long(0),
(Long sum0, long eachKey, int eachValue) ->
{
return new Long((long) (sum0 + eachKey + eachValue));
});
Assert.assertEquals(new Long(10), sum);
}

In this example, we inject the value 0 and apply the three-argument summation function to each key and value. This allows us to calculate the sum of all key-values in the map!

Example: Product

@Test
public void injectIntoKeyValueProduct()
{
LongIntMap map = this.newWithKeysValues(1L, 2, 3L, 4);
Long sum = map.injectIntoKeyValue(new Long(1),
(Long sum0, long eachKey, int eachValue) ->
{
return new Long((long) (sum0 * eachKey * eachValue));
});
Assert.assertEquals(new Long(24), sum);
}

For this example, we inject the value 1 and apply the three-argument product function to each key and value. The result is the product of all key-values in the map!

Example: Primitive to Object Map

@Test
public void injectIntoKeyValueCopy()
{
IntObjectMap map1;
MutableMap<?, ?> map2 = map1.injectIntoKeyValue(
Maps.mutable.empty(), target::withKeyValue);
}

Here, we can see how to copy a primitive map to an object map. This example takes an IntObjectMap and copies its keys and values to a Map<Int, Object>. Fun fact: a question involving exactly this is what inspired the feature to be implemented!

Implementation

This new feature was implemented for object and primitive maps; it can be applied to PrimitivePrimitiveMaps, PrimitiveObjectMaps, ObjectPrimitiveMaps, and ObjectObjectMaps. Since Java does not support generics for primitives, implementation on the primitive side was a bit tedious. For the three-argument function taken in by injectIntoKeyValue, several function templates needed to be defined in order to generate every possible function permutation. One of the templates, ObjectObjectPrimitiveToObjectFunction, can be viewed here. If you’re interested in learning more about the need for universal generics in Java, I suggest checking out Project Valhalla.

This new API will be available in the Eclipse Collections 11.1.0 release! For more information on Eclipse Collections, check out the repo on GitHub.

Sign up to discover human stories that deepen your understanding of the world.

--

--

Emilie Robichaud
Emilie Robichaud

Written by Emilie Robichaud

University of Toronto graduate with majors in Computer Science and Mathematics! Always eager to explore more in the world of technology.

No responses yet

What are your thoughts?