Easy Tuples for Java


Overview

With Java 16 providing us with records as a standard feature we now have a way of creating tuples in Java. However, these are ‘nominal’ tuples - in other words the tuples need to be named.

In some situations (where the scope of the tuple will only exist within a single method body, for example) it is just more convenient to not bother naming the tuple and view it instead structurally - for example as a pair (2-tuple) or triplet (3-tuple) or otherwise.

Enter the easy-tuples library…

easy-tuples provides easy and convenient generic records representing tuples with one to ten fields and with a consistent naming. You can use easy-tuples in the situation where it makes code more readable to remove the ‘nominal’ and think ‘structural’.

// Create a pair and a triplet...       // toString():
var d1 = _2.of(25, "December");         // _2[_1=25, _2=December]
var d2 = _3.of(12, "January", 2021);    // _3[_1=12, _2=January, _3=2021]

// Append the missing year on d1.
var d1Updated = d1.append(2021);        // _3[_1=25, _2=December, _3=2021]

// Get the month of d1 and use a setter
// of d2 to make them equal. 
var d1Month = d1._2();  // Use the getter for the field 
var d2Updated = d2.set2(d1Month);       // _3[_1=12, _2=December, _3=2021]

Usage

Dependency Coordinates

easy-tuples is available on maven-central: TODO

API

Each tuple exposes an intuitive, uniform API:

Suggested Use

Non suggested use

It is probably best to avoid using these tuples:

FAQs