This post provides an overview of the Pair<A,B> class provided by the javatuples library for Java and discuss some of its common methods.

A pair is a group of two related or unrelated objects, i.e., objects with or without any meaningful relationship. For example, consider [A, 1], a pair containing a string and an integer, which can be anything. The javatuples library is among the top Java libraries, along with Apache Commons Lang, that provides an implementation of the Pair class. But unlike Apache Commons, the javatuples Pair class offers tons of methods and has several other tuple classes.

 
In this post, we’ll illustrate working of Pair<A,B> class. Javatuples also offers the KeyValue<A,B> and LabelValue<A,B> class to implement pairs in Java, which are more or less the same. All these classes are Type safe, Immutable, Serializable, Comparable, and also implements equals(), hashCode(), and toString() methods.

1. Initialization

There are several ways to get an instance of the Pair class of javatuples. To create a Pair object, we can either call its constructor with two arguments of any type, or use the Pair.with() method that takes the same arguments and returns a new Pair. For example:

Download  Run Code

 
We can use the Pair.fromArray() method to create a Pair of the same type from an array with exactly two elements. However, this method will throw an IllegalArgumentException if the array has more than or less than two elements. For example:

Download  Run Code

 
To create tuples from a collection, we can use the fromCollection() method. Similarly, we can use the fromIterable() method to make a tuple from any iterable. However, both methods require that the collection or iterable has exactly two elements for a Pair. If the iterable has more than two elements, we can use another version of the fromIterable() method that takes an index as a parameter and returns two values from the iterable starting at that index. For example:

Download  Run Code

2. Getters and Setters

The javatuples Pair class provides specific methods for retrieving the value of the first and the second element. To access the first and second element of a Pair class, we can use the methods getValue0() and getValue1(), respectively. These methods return the value at the position they indicate in the pair. We don’t need to cast the returned value, as long as we have specified the parameterized types while creating the Pair class. i.e., like Pair<A,B>.

Download  Run Code

 
To get an object at a given position in the Pair class, we can also use the getValue() method with an integer argument. We need to cast the returned object to the appropriate type, as it is an object type. For example:

Download  Run Code

 
To set or update the values at the first and second positions in a pair, we can use the methods setAt0() and setAt1(), respectively. Since pairs are immutable, these methods return a new pair with the updated values, and we need to store the returned pair in a variable.

Download  Run Code

That’s all about Pair class offered by Javatuples library.