Java Pair class stores value in the key-value pair combination. It is useful for getting two values. This key-value combination is known as tuples.
The pairs are useful when we want two values to be returned from a method. For example, if we have a method that calculates the square root of a number and we want to print the number with its square root. Thus, we can print the output in a combination of numbers and its square root (e.g., (5, 25)) using the pair class.
Java provides an inbuilt Pair class from Java 8.
In this section, we will see how to use the Pair class in Java. Further, we will create our customized Pair class.
Pair Class in Java
Since Java 8, Java provides a Pair class to store the values in key pair combinations. To store the value in pair combination, we need to use the parameterized constructor provided by the javafx.util.Pair class.
The Pair class is useful while storing the value in a tree data structure. While performing the recursion operation on a tree data structure, it is easy to pass value from one node to another node using the Pair class. These values can be minimum and maximum node values up to that node. This would prevent the pointer from revisiting certain codes repeatedly.
In Java, the Maps are used to store the value in the form of a key pair combination. They store the value as tuples and operate them as a single object.
To implement the Pair class, we need to import its parent package:
Inbuilt pair class uses the following notation of Key and Value just like the Map in Java to store a pair:
The Pair class object is declared as follows:
The above syntax will create an object of the type <Integer, String> and the constructor will take the value 5 and "Five" and store them in the Pair class.
Pair Class Methods
The following methods are provided by the Java Pair class:
It gets the key for the given pair and returns the key for the specified pair. It is declared as follows:
It gets the value for the given pair and returns the value of the pair. It is declared as follows:
It generates the hash code for the pair; this hashcode is calculated using both the name and value of the pair. It overrides the HashCode class and returns the hash code for the pair. It is declared as follows:
It is used to test the pair for equality with the other pair. If the object is not specified for the testing or is null, then it will return false. The given two pairs will be considered as equal if and only if their names and values are the same. It is declared as follows:
It takes the "-o" arguments object to test for the equality with the pair. It overrides the equals in the class Objects and returns true if the given pair is equal; otherwise, it will return false.
It is used to represent the pair as String. In toString() method the default name/value delimiter=' is always used. It overrides the toString in class Object and returns the String value of the pair. It is declared as follows:
Pair Class Constructor
The Pair class constructor creates a new pair. It takes the key and pair values as an argument. It is declared as follows:
Types of Pair Classes
There are two types of Pair classes in Java, which are as follows:
Why do We Need Pair Class
The pair class is used to get the value in a key pair combination. In other terms, we can say that the pair class provides methods that return two values together. There may be lots of reasons why we use the Pair class.
The following are few cases when we need to use the Pair class:
Example of Pair Class
Let's understand it with a basic example:
Now, create a class file under the src folder.
We are creating a TestPair class.
In the above program, we are taking a pair value that holds an integer key value and a String pair value. We are declaring a Pair class using its standard syntax and printing its value using the getKey() and getValue() method of the pair class. It will display the following output:
The key is :5 The Pair value is :Five
Customized Pair Class
If you don't want to use the inbuilt Pair class, you can customize a Pair class using Generics. Consider the below example:
From the above code, we have created a Customized Pair class and store two values in a single variable and print it.
< 1, Hello >
Hence, using generics methods, we can take the benefits of a Pair class without using the JavaFX.
Let's understand its behavior with functions:
Pair Class with Functions
We can use a Pair class to return two values using a function. To perform this process, the return type of function must be a pair. Consider the below example:
Enter Value Five Enter Key 5 < 5, Five >
Pair Classes are very useful for improving code efficiency. One useful case of the Pair class is to store the value in the tree data structure. We can use inbuilt Pair classes or customize our classes. If they are used properly, they will be very helpful.