What is a Pair Class in Java? With Examples

Java Pair class stores two values in the form of a tuple. This can be useful to get a function to return two values.

Java has an inbuilt Pair class from Java 8 onwards. That is why in this lesson we will not only look at the inbuilt pair class but also learn how to make our own pair class.

Pair class is very useful when using a Tree data structure. While using recursion on a tree, pair class makes it easy to pass values from one node to another. These values can be minimum and maximum node value up to that node. This would prevent revisiting certain nodes again and again.

In-built Pair class in Java

Inbuilt Pair class is a part of the JavaFX package. However, this package is not part of the standard OpenJDK installation. We can add it to our project using the following maven dependencies.

Let’s get right to the usage of the inbuilt pair classes in Java.

Importing Pair class

To import use:

Inbuilt pair class uses the notation of Key and Value to store a pair.

However, don’t confuse it with HashMap.

Declaring a Pair object

This creates a pair object of the type <Integer, String> .

The constructor takes the values 7 and “hello” and stores them in the Pair class.

Accessing values

We can access values from Pair object using getKey() and getValue() methods.

  • getKey gets the first value.
  • getValue gets the second value.

Output

The complete code for this section is :

Creating our own Pair class

Creating our own pair class is a better alternative as we can customize it according to our own preference. Also, if you are not using JavaFX in your project, it makes sense to avoid it completely.

1. Defining the Custom Pair class

For the purpose of simplicity, we are using a pair class that stores two values. An Integer and a String. Using generics would be a good idea too.

Two variables first and second have been defined to store two values of the type int and String respectively.

The constructor takes two values and assigns them to the variables.

2. Pair class in action

To use the pair class just created, define an object of the pair class and call the constructor.

This creates a pair object with 1 stored in first and “one” stored in second.

To access the values :

Output

Since pair class is defined by us, we can customize and add a print function to it.

Code for pair class :

output

Including a print function, this way is a good coding habit as now the variables could be made private. Keeping the variables public is not safe.

Using Pair class with a function

Pair class can be used to return two values from a function. To do this, the return type of the function must be pair.

The object that is returned from this function can be captured in the main function. Let’s print the object in the main.

Output

The complete code for this section is as follows.

Conclusion

Pair classes are important and can improve the efficiency of the code if used properly. One interesting area to use pair class is to store the maximum and minimum values of a subtree in BST problems. This saves the multiple traversals down the tree.

By admin

Leave a Reply

%d bloggers like this: