Javatuples Library – How to use org.javatuples.Pair Class

This post provides an overview of Pair<A,B> class provided by 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 between them. For example consider ["A", 1], which is a pair containing a String and an Integer, which can be anything.

 
javatuples 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 will illustrate working of Pair<A,B> class, but we can also use 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 methods to get an instance of javatuples’s Pair class –

 

1.1. Pair.with()

The standard way to initalize a Pair is to use with() method, that accepts two objects of same or different type and return a Pair instance.

 

1.2. Constructors

Pair class also offers a constructor that takes two arguments of same or different type, from which a pair can be constructed.

 

1.3. Pair.fromArray()

Array having exactly 2 elements can be used to create a Pair (of same Type) using fromArray() method of Pair class as shown below. Please note that an IllegalArgumentException will be thrown if the array contains more than or less than 2 elements.

 

1.4. Pair.fromIterable()

We can also create tuples from collections using fromCollection() method or more generally, any iterable using fromIterable() method as shown below.

Like for an array, the collection or iterable must have exactly 2 elements in order to create a Pair. For an iterable having more than 2 elements, we can retrieve two values from an iterable starting at a specific index by using an overloaded version of fromIterable() method as shown below:

 

2. Getters:

Pair class provides specific methods for retrieving the value of the first and the second element. These methods are named as getValue0() and getValue1() based on the position of the value they return inside the pair. Both these methods are completely typesafe, so no casting is required. But this is true only if we have used the parameterized types while constructing the Pair class i.e. like Pair<A,B>.

 

 
Pair class also offers getValue(int) method, which take a integer as an argument, and return an Object at that position in the Pair. It should not be used as an Object Type is returned and we’ll need a cast as shown below:

 
Please note that the KeyValue and LabelValue classes have their getValue0()/getValue1() methods renamed as getKey()/getValue() and getLabel()/getValue(), respectively.

 

3. Setters:

In order to set new values at first and second position in a pair, we can use the setAt0() and setAt1() setter method respectively. Since pairs are immutable, we will have to collect the return value of your setAtX(...) calls as shown below:

 
References: Javatuples Library

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz