EC by Examples: Getting and Putting values in the Primitive Map API.

Alex Goldberg
2 min readApr 4, 2022

I have recently contributed to Eclipse Collections project adding a getAndPut method to the Primitive Map API.

What is a Primitive Map?

Primitive Maps are a part of memory optimized Primitive Collections in Eclipse Collections library.

Primitive Collections do not require boxing and unboxing of the primitive types compared to traditional object based collections (List, Map, etc). Because of this using Primitive Collections can give us speed boost and reduce memory footprint.

Refer to this tutorial to learn how to use Primitive Collections by example.

The problem with primitive put

Note that put method returns void and does not allow to retrieve a previous value associated with a key.

A one stop shop for getting and putting

The new method returns a previous value or a defaultValue if there was no previous mapping for akey. It also associates a putValuewith akey.

How do I use it?

The image above demonstrates how to use getAndPutand what to expect it to return when replacing a value for an existing key and when adding a new map entry.

What interfaces is getAndPut() available on?

The new method has been added to all mutable primitive maps. Bellow is a partial list of primitive maps it has been added to.

The new APIs methods will be available in the next release of EC (11.1.0).

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

--

--