DEV Community

Cover image for Eclipse Collections now supports Triples
1 1

Eclipse Collections now supports Triples

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);
Enter fullscreen mode Exit fullscreen mode

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
       .select(Triple::getThree)
       .collect(Triple::getOne);

euCountries.forEach(System.out::println);
Enter fullscreen mode Exit fullscreen mode

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

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide