Five Alternatives to Pair Class in Java

A Pair is a container to store a tuple of two objects. Java doesn’t really provide any implementation of the Pair class. In this post, we will discuss about various alternatives to Pair class in Java.

 
Pair is often useful to track two objects together. It contains two fields, usually termed as first and second, that are capable of storing anything. In spite of not having any meaningful relationship between the first and second field, programmers often miss this functionality in Java.

In the previous post, we have discussed how to implement our own Pair Class in Java. In this post, we will discuss workarounds available in Java to fill the desired gap, as discussed below:

 

1. Map.Entry<K,V> Interface

We can use Map.Entry<K,V> interface in Java which is similar to std::pair in C++. It is an excellent example of having a meaningful name which represent a key-value pair.

To create an entry representing a mapping from the specified key to the specified value, Java provides two concrete implementations of the Map.Entry interface, namely, AbstractMap.SimpleEntry and AbstractMap.SimpleImmutableEntry.

 

Download   Run Code

Output:

[Java=50, C++=30]
50

 

2. Java 8 – javafx.util.Pair

Finally after a long wait, a Pair<K,V> class is added in Java 8 in javafx.util package. The class represent key-value pairs and supports very basic operations like getKey(), getValue(), hashCode(), equals(java.lang.Object o) and toString() and has few methods inherited from java.lang.Object class. Well something is better than nothing, huh?.

 

Download Code

Output:

{C, 20}
{C++, 30}

 

3. Apache Commons Lang Library

Apache Commons Lang Library also provides a Pair<L,R> utility class whose elements are left and right. It is defined as abstract and implements the Map.Entry interface where the key is left and the value is right.

It has Pair.of() method that can be used to obtain an immutable pair of from specified pair of objects. Its subclass MutablePair is mutable whereas ImmutablePair is immutable. However, the type of the object stored in ImmutablePair can itself may be mutable.

 

Download Code

Output:

[(C,20), (C++,30), (Java,50)]
C, 100
UnsupportedOperationException thrown
C++, 30
UnsupportedOperationException thrown
Java, 50

 

4. JavaTuples Library

Javatuples is another famous and simple java library that deals with tuples. It provides a set of tuple java classes ranging from one to ten elements. To serve our purpose, we can use Pair<A,B> class.

 

Download Code

Output:

{Java, 50}
{C++, 30}

 

5. Collections.singletonMap()

Another approach is to use Collections.singletonMap() in Java which is similar to Map.Entry<K,V> discussed earlier. It returns an immutable singleton map containing only the specified key-value pair mapping, which can be treated as a Pair.

 

Download   Run Code

Output:

[{Java=50}, {C++=30}]

 
Finally in Android projects, we can use android.util.Pair class provided by the Android SDK.

 
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