Cover image for Eclipse Collections now supports Triples

Eclipse Collections now supports Triples

brianverm profile image Brian Vermeer 🧑🏼‍🎓🧑🏼‍💻 ・2 min read

I have recently contributed to the Eclipse Collections framework by implementing a new datatype called Triple. It will be part of release 10.3.0

What is a Triple

A Triple is an implementation of a Tuple that can hold 3 arbitrary values. Tuples and therefore the Triple as well are immutable finite collections with the sole purpose of grouping data together. Tuples are well known and well-used data types in computer science and are natively implemented in many languages Haskell, Erlang and Smalltalk but also Python and C#. The only thing in Java that looks like a tuple implementation is Map.Entry<K,V>.

A Tuple in his purest form is not bound to hold only 2 items, better known as the 2-tuple. In Eclipse Collections there already was a 2-tuple implementation called Pair. This could hold exactly 2 elements grouped together. In the languages mentioned above, you can hold create Tuples holding 3,4,5 or even more elements. It turned out that using a tuple holding more than 3 items are rarely used. However, 2 items in some cases are just not enough. Hence the Triple implementation for Eclipse Collections

How does it work

A Triple can hold 3 different types of objects grouped together. If you want to hold 3 objects of the same type there is a similar type called Triplet where the type of all three elements is the same.

In the following example, I am grouping a country name with its international dialing code and if they are a member of the EU (European Union). Effectively I bundle 3 types: String, int, and boolean.

Tuples.triple("Netherlands",  31, true);
Tuples.triple("United Kingdom",  44, false);
Tuples.triple("Germany", 49, true);

To get the values out of a Triple you can use getOne(), getTwo() and getThree() to obtain the respective values.
Say I have a list of European countries and I only want to print the name of the countries that are EU members. Using a Triple to implement this with Eclipse Collections, the code could like this.

var countries = Lists.immutable.of(
       Tuples.triple("Netherlands", 31, true),
       Tuples.triple("United Kingdom", 44, false),
       Tuples.triple("Germany", 49, true)

var euCountries = countries


Note I am using Java 11 in this example and the type of countries is ImmutableTripletonList

A Triple can be useful in many other scenarios. You can think of adding meta information to a calculation like start time and end time. Returning a Triple would be a great choice to achieve this. And sometimes, you just need to return more than a single result from a method.

Of course, there are a bunch of other ways you can achieve the same result. Just consider a Triple another tool in the toolbox you can use if you prefer. More importantly, programmers have different backgrounds and providing them with tools they prefer to work with like multi-value tuples make Java more accessible. Even if you are not able to work with the latest Java version.

Demo code is available on Github

Posted on by:

brianverm profile

Brian Vermeer 🧑🏼‍🎓🧑🏼‍💻


Java Dev | DevRel | VirtualJug Co-lead | UtrechtJUG Co-lead | MyDevSecOps Co-lead | Dutch Air Reserve | Taekwondo Master | Flag Football CB/WR


markdown guide